home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / doc / Tcl.n < prev    next >
Text File  |  1993-02-14  |  116KB  |  2,820 lines

  1. '\"
  2. '\" Copyright 1989-1992 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. .so man.macros
  11. .HS Tcl tcl
  12. .BS
  13. .SH NAME
  14. Tcl \- overview of tool command language facilities
  15. .BE
  16.  
  17. .SH INTRODUCTION
  18. .PP
  19. Tcl stands for ``tool command language'' and is pronounced ``tickle.''
  20. It is actually two things:
  21. a language and a library.
  22. First, Tcl is a simple textual language,
  23. intended primarily for issuing commands to interactive programs such
  24. as text editors, debuggers, illustrators, and shells.  It has
  25. a simple syntax and is also programmable, so
  26. Tcl users can write command procedures to provide more powerful
  27. commands than those in the built-in set.
  28. .PP
  29. Second, Tcl is a library package that can be embedded in application
  30. programs.  The Tcl library consists of a parser for the Tcl
  31. language, routines to implement the Tcl built-in commands, and
  32. procedures that allow each application to extend Tcl with additional
  33. commands specific to that application.  The application program
  34. generates Tcl commands and passes them to the Tcl parser for
  35. execution.  Commands may be generated
  36. by reading characters from an input
  37. source, or by associating command strings with elements of the
  38. application's user interface, such as menu entries, buttons, or
  39. keystrokes.
  40. When the Tcl library receives commands it parses them
  41. into component fields and executes built-in commands directly.
  42. For commands implemented by the
  43. application, Tcl calls back to the application to execute the
  44. commands.  In many cases commands will invoke recursive invocations
  45. of the Tcl interpreter by passing in additional strings to execute
  46. (procedures, looping commands, and conditional commands all work
  47. in this way).
  48. .PP
  49. An application program gains three advantages by using Tcl for
  50. its command language.  First, Tcl provides a standard syntax:  once
  51. users know Tcl, they will be able to issue commands easily
  52. to any Tcl-based application.  Second, Tcl provides programmability.
  53. All a Tcl application needs to do is to implement a few
  54. application-specific low-level commands.  Tcl provides many utility
  55. commands plus a general programming interface for building up
  56. complex command procedures.  By using Tcl, applications need not
  57. re-implement these features.  Third, Tcl can be used as
  58. .VS
  59. a common language for communicating between applications.
  60. Inter-application communication is not built into the Tcl core
  61. described here, but various add-on libraries, such as the Tk toolkit,
  62. allow applications to issue commands to each other.
  63. This makes it possible for applications to work together in much
  64. more powerful ways than was previously possible.
  65. .VE
  66. .PP
  67. This manual page focuses primarily on the Tcl language.  It describes
  68. the language syntax and the built-in commands that will be available in
  69. any application based on Tcl.  The individual library
  70. procedures are described in more detail in separate manual pages, one
  71. per procedure.
  72.  
  73. .SH "INTERPRETERS"
  74. .PP
  75. The central data structure in Tcl is an interpreter (C type
  76. ``Tcl_Interp'').  An interpreter consists of a set of command
  77. bindings, a set of variable values, and a few other miscellaneous
  78. pieces of state.  Each Tcl command is interpreted in the context
  79. of a particular interpreter.
  80. Some Tcl-based applications will maintain
  81. multiple interpreters simultaneously, each associated with a
  82. different widget or portion of the application.
  83. Interpreters are relatively lightweight structures.  They can
  84. be created and deleted quickly, so application programmers should feel free to
  85. use multiple interpreters if that simplifies the application.
  86. Eventually Tcl will provide a mechanism for sending Tcl commands
  87. and results back and forth between interpreters, even if the
  88. interpreters are managed by different processes.
  89.  
  90. .SH "DATA TYPES"
  91. .PP
  92. Tcl supports only one type of data:  strings.  All commands,
  93. all arguments to commands, all command results, and all variable values
  94. are strings.
  95. Where commands require numeric arguments or return numeric results,
  96. the arguments and results are passed as strings.
  97. Many commands expect their string arguments to have certain formats,
  98. but this interpretation is
  99. up to the individual commands.  For example, arguments often contain
  100. Tcl command strings, which may get executed as part of the commands.
  101. The easiest way to understand the Tcl interpreter is to remember that
  102. everything is just an operation on a string.  In many cases Tcl constructs
  103. will look similar to more structured constructs from other languages.
  104. However, the Tcl constructs
  105. are not structured at all; they are just strings of characters, and this
  106. gives them a different behavior than the structures they may look like.
  107. .PP
  108. Although the exact interpretation of a Tcl string depends on who is
  109. doing the interpretation, there are three common forms that strings
  110. take:  commands, expressions, and lists.  The major sections below
  111. discuss these three forms in more detail.
  112.  
  113. .SH "BASIC COMMAND SYNTAX"
  114. .PP
  115. The Tcl language has syntactic similarities to both the Unix shells
  116. and Lisp.  However, the interpretation of commands is different
  117. in Tcl than in either of those other two systems.
  118. A Tcl command string consists of one or more commands separated
  119. by newline characters or semi-colons.
  120. Each command consists of a collection of fields separated by
  121. white space (spaces or tabs).
  122. The first field must be the name of a command, and the
  123. additional fields, if any, are arguments that will be passed to
  124. that command.  For example, the command
  125. .DS
  126. \fBset a 22\fR
  127. .DE
  128. has three fields:  the first, \fBset\fR, is the name of a Tcl command, and
  129. the last two, \fBa\fR and \fB22\fR, will be passed as arguments to
  130. the \fBset\fR command.  The command name may refer either to a built-in
  131. Tcl command, an application-specific command bound in with the library
  132. procedure \fBTcl_CreateCommand\fR, or a command procedure defined with the
  133. \fBproc\fR built-in command.
  134. Arguments are passed literally as
  135. text strings.  Individual commands may interpret those strings in any
  136. fashion they wish.  The \fBset\fR command, for example, will treat its
  137. first argument as the name of a variable and its second argument as a
  138. string value to assign to that variable.  For other commands arguments
  139. may be interpreted as integers, lists, file names, or Tcl commands.
  140. .PP
  141. .VS
  142. Command names should normally be typed completely (e.g. no abbreviations).
  143. However, if the Tcl interpreter cannot locate a command it invokes a
  144. special command named \fBunknown\fR which attempts to find or create
  145. the command.
  146. For example, at many sites \fBunknown\fR will search
  147. through library directories for the desired command and create it
  148. as a Tcl procedure if it is found.
  149. The \fBunknown\fR command often provides automatic completion of
  150. abbreviated commands, but usually only for commands that were typed
  151. interactively.
  152. It's probably a bad idea to use abbreviations in command scripts
  153. and other forms that will be re-used over time:  changes
  154. to the command set may cause abbreviations to become ambiguous,
  155. resulting in scripts that no longer work.
  156. .VE
  157.  
  158. .SH "COMMENTS"
  159. .PP
  160. If the first non-blank character in a command is \fB#\fR, then everything
  161. from the \fB#\fR up through the next newline character is treated as
  162. a comment and ignored.  When comments are embedded inside nested
  163. commands (e.g. fields enclosed in braces) they must have properly-matched
  164. braces (this is necessary because when Tcl parses the top-level command
  165. it doesn't yet know that the nested field will be used as a command so
  166. it cannot process the nested comment character as a comment).
  167.  
  168. .SH "GROUPING ARGUMENTS WITH DOUBLE-QUOTES"
  169. .PP
  170. Normally each argument field ends at the next white space, but
  171. double-quotes may be used to create arguments with embedded
  172. space.  If an argument
  173. field begins with a double-quote, then the argument isn't
  174. terminated by white space (including newlines) or a semi-colon
  175. (see below for information on semi-colons); instead it ends at the next
  176. double-quote character.  The double-quotes are not included
  177. in the resulting argument.  For example, the
  178. command
  179. .DS
  180. \fBset a "This is a single argument"\fR
  181. .DE
  182. will pass two arguments to \fBset\fR:  \fBa\fR and
  183. \fBThis is a single argument\fR.  Within double-quotes, command
  184. substitutions, variable substitutions, and backslash substitutions
  185. still occur, as described below.  If the first character of a
  186. command field is not a quote, then quotes receive no special
  187. interpretation in the parsing of that field.
  188.  
  189. .SH "GROUPING ARGUMENTS WITH BRACES"
  190. .PP
  191. Curly braces may also be used for grouping arguments.  They are
  192. similar to quotes except for two differences.  First, they nest;
  193. this makes them easier to use for complicated arguments like nested Tcl
  194. command strings.  Second, the substitutions described below for
  195. commands, variables, and backslashes do \fInot\fR occur in arguments
  196. enclosed in braces, so braces can be used to prevent substitutions
  197. where they are undesirable.
  198. If an argument field
  199. begins with a left brace, then the argument ends at the matching
  200. right brace.  Tcl will strip off the outermost layer of braces
  201. and pass the information between the braces to the command without
  202. any further modification.  For example, in the command
  203. .DS
  204. \fBset a {xyz a {b c d}}\fR
  205. .DE
  206. the \fBset\fR command will receive two arguments: \fBa\fR
  207. and \fBxyz a {b c d}\fR.
  208. .PP
  209. When braces or quotes are in effect, the matching brace
  210. or quote need not be on
  211. the same line as the starting quote or brace; in this case
  212. the newline will be
  213. included in the argument field along with any other characters up to the
  214. matching brace or quote.  For example, the \fBeval\fR command
  215. takes one
  216. argument, which is a command string; \fBeval\fR invokes the Tcl
  217. interpreter to execute the command string.  The command
  218. .DS
  219. \fBeval {
  220.     set a 22
  221.     set b 33
  222. }\fR
  223. .DE
  224. will assign the value \fB22\fR to \fBa\fR and \fB33\fR to \fBb\fR.
  225. .PP
  226. If the first character of a command field is not a left
  227. brace, then neither left nor right
  228. braces in the field will be treated specially (except as part of
  229. variable substitution; see below).
  230.  
  231. .SH "COMMAND SUBSTITUTION WITH BRACKETS"
  232. .PP
  233. If an open bracket occurs in a field of a command, then
  234. command substitution occurs (except for fields enclosed in
  235. braces).  All of the text up to the matching
  236. close bracket is treated as a Tcl command and executed immediately.
  237. Then the result of that command is substituted for the bracketed
  238. text.  For example, consider the command
  239. .DS
  240. \fBset a [set b]\fR
  241. .DE
  242. When the \fBset\fR command has only a single argument, it is the
  243. name of a variable and \fBset\fR returns the contents of that
  244. variable.  In this case, if variable \fBb\fR has the value \fBfoo\fR,
  245. then the command above is equivalent to the command
  246. .DS
  247. \fBset a foo\fR
  248. .DE
  249. Brackets can be used in more complex ways.  For example, if the
  250. variable \fBb\fR has the value \fBfoo\fR and the variable \fBc\fR
  251. has the value \fBgorp\fR, then the command
  252. .DS
  253. \fBset a xyz[set b].[set c]\fR
  254. .DE
  255. is equivalent to the command
  256. .DS
  257. \fBset a xyzfoo.gorp\fR
  258. .DE
  259. .VS
  260. A bracketed command may contain multiple commands separated by
  261. newlines or semi-colons in the usual fashion.
  262. In this case the value of the last command is used for substitution.
  263. For example, the command
  264. .DS
  265. \fBset a x[set b 22
  266. expr $b+2]x\fR
  267. .DE
  268. is equivalent to the command
  269. .DS
  270. \fBset a x24x\fR
  271. .DE
  272. .VE
  273. If a field is enclosed in braces then the brackets and the characters
  274. between them are not interpreted specially; they are passed through
  275. to the argument verbatim.
  276.  
  277. .SH "VARIABLE SUBSTITUTION WITH $"
  278. .PP
  279. The dollar sign (\fB$\fR) may be used as a special shorthand form
  280. for substituting variable values.
  281. If \fB$\fR appears in an argument that isn't enclosed in braces
  282. then variable substitution will occur.  The characters after
  283. the \fB$\fR, up to the first character that isn't a number, letter, or
  284. underscore, are taken as a variable name and the string value of that
  285. variable is substituted for the name.
  286. .VS
  287. For example, if variable \fBfoo\fR
  288. has the value \fBtest\fR, then the command
  289. .DS C
  290. \fBset a $foo.c\fR
  291. .DE
  292. is equivalent to the command
  293. .DS C
  294. \fBset a test.c\fR
  295. .DE
  296. .PP
  297. There are two special forms for variable substitution.
  298. If the next character after the name of the variable is an
  299. open parenthesis, then the variable is assumed to be an array
  300. name, and all of the characters between the open parenthesis
  301. and the next close parenthesis are taken as an index into the array.
  302. Command substitutions and variable substitutions are
  303. performed on the information between the parentheses before it is
  304. used as an index.
  305. For example, if the variable \fBx\fR is an array with one element
  306. named \fBfirst\fR and value \fB87\fR and another element named
  307. \fB14\fR and value \fBmore\fR, then the command
  308. .DS C
  309. \fBset a xyz$x(first)zyx
  310. .DE
  311. is equivalent to the command
  312. .DS C
  313. \fBset a xyz87zyx\fR
  314. .DE
  315. If the variable \fBindex\fR has the value \fB14\fR, then the command
  316. .DS C
  317. \fBset a xyz$x($index)zyx
  318. .DE
  319. is equivalent to the command
  320. .DS C
  321. \fBset a xyzmorezyx
  322. .DE
  323. For more information on arrays, see VARIABLES AND ARRAYS below.
  324. .PP
  325. The second special form for variables occurs when
  326. the dollar sign is followed by an open curly brace.
  327. In this case the variable name consists of all the characters
  328. up to the next curly brace.
  329. Array references are not possible in this form:  the name
  330. between braces is assumed to refer to a scalar variable.
  331. For example, if variable \fBfoo\fR has the value \fBtest\fR,
  332. then the command
  333. .DS C
  334. \fBset a abc${foo}bar\fR
  335. .DE
  336. is equivalent to the command
  337. .DS C
  338. \fBset a abctestbar\fR
  339. .DE
  340. .VE
  341. Variable substitution does not occur in arguments that are enclosed
  342. in braces:  the
  343. dollar sign and variable name are passed through to the argument verbatim.
  344. .PP
  345. The dollar sign abbreviation is simply a shorthand form.  \fB$a\fR is
  346. completely equivalent to \fB[set a]\fR; it is provided as a convenience
  347. to reduce typing.
  348.  
  349. .SH "SEPARATING COMMANDS WITH SEMI-COLONS"
  350. .PP
  351. Normally, each command occupies one line (the command is terminated by
  352. a newline character).  However, semi-colon (``;'') is treated
  353. as a command separator character; multiple commands may be placed
  354. on one line by separating them with a semi-colon.  Semi-colons are
  355. not treated as command separators if they appear within curly braces
  356. or double-quotes.
  357.  
  358. .SH "BACKSLASH SUBSTITUTION"
  359. .PP
  360. Backslashes may be used to insert non-printing characters into
  361. command fields and also to insert special characters like
  362. braces and brackets into fields
  363. without them being interpreted specially as described above.
  364. The backslash sequences understood by the Tcl interpreter are
  365. listed below.  In each case, the backslash
  366. sequence is replaced by the given character:
  367. .TP 20
  368. \fB\eb\fR
  369. Backspace (0x8).
  370. .TP 20
  371. \fB\ef\fR
  372. Form feed (0xc).
  373. .TP 20
  374. \fB\en\fR
  375. Newline (0xa).
  376. .TP 20
  377. \fB\er\fR
  378. Carriage-return (0xd).
  379. .TP 20
  380. \fB\et\fR
  381. Tab (0x9).
  382. .TP 20
  383. \fB\ev\fR
  384. Vertical tab (0xb).
  385. .TP 20
  386. \fB\e{\fR
  387. Left brace (``{'').
  388. .TP 20
  389. \fB\e}\fR
  390. Right brace (``}'').
  391. .TP 20
  392. \fB\e[\fR
  393. Open bracket (``['').
  394. .TP 20
  395. \fB\e]\fR
  396. Close bracket (``]'').
  397. .TP 20
  398. \fB\e$\fR
  399. Dollar sign (``$'').
  400. .TP 20
  401. \fB\e<space>\fR
  402. Space (`` ''): doesn't terminate argument.
  403. .br
  404. .TP 20
  405. \fB\e;\fR
  406. Semi-colon: doesn't terminate command.
  407. .TP 20
  408. \fB\e"\fR
  409. Double-quote.
  410. .TP 20
  411. \fB\e<newline>\fR
  412. Nothing:  this joins two lines together
  413. into a single line.  This backslash feature is unique in that
  414. it will be applied even when the sequence occurs within braces.
  415. .TP 20
  416. \fB\e\e\fR
  417. Backslash (``\e'').
  418. .TP 20
  419. \fB\e\fIddd\fR
  420. The digits \fIddd\fR (one, two, or three of them) give the octal value of
  421. the character.  Null characters may not be embedded in command fields;
  422. if \fIddd\fR is zero then the backslash sequence is ignored (i.e. it
  423. maps to an empty string).
  424. .PP
  425. For example, in the command
  426. .DS
  427. \fBset a \e{x\e[\e\0yz\e141\fR
  428. .DE
  429. the second argument to \fBset\fR will be ``\fB{x[\0yza\fR''.
  430. .PP
  431. If a backslash is followed by something other than one of the options
  432. described above, then the backslash is transmitted to the argument
  433. field without any special processing, and the Tcl scanner continues
  434. normal processing with the next character.  For example, in the
  435. command
  436. .DS
  437. \fBset \e*a \e\e\e{foo\fR
  438. .DE
  439. The first argument to \fBset\fR will be \fB\e*a\fR and the second
  440. argument will be \fB\e{foo\fR.
  441. .PP
  442. If an argument is enclosed in braces, then backslash sequences inside
  443. the argument are parsed but no substitution occurs (except for
  444. backslash-newline):  the backslash
  445. sequence is passed through to the argument as is, without making
  446. any special interpretation of the characters in the backslash sequence.
  447. In particular, backslashed braces are not counted in locating the
  448. matching right brace that terminates the argument.
  449. For example, in the
  450. command
  451. .DS
  452. \fBset a {\e{abc}\fR
  453. .DE
  454. the second argument to \fBset\fR will be \fB\e{abc\fR.
  455. .PP
  456. This backslash mechanism is not sufficient to generate absolutely
  457. any argument structure; it only covers the
  458. most common cases.  To produce particularly complicated arguments
  459. it is probably easiest to use the \fBformat\fR command along with
  460. command substitution.
  461.  
  462. .SH "COMMAND SUMMARY"
  463. .IP [1]
  464. A command is just a string.
  465. .IP [2]
  466. Within a string commands are separated by newlines or semi-colons
  467. (unless the newline or semi-colon is within braces or brackets
  468. or is backslashed).
  469. .IP [3]
  470. A command consists of fields.  The first field is the name of the command.
  471. The other fields are strings that are passed to that command as arguments.
  472. .IP [4]
  473. Fields are normally separated by white space.
  474. .IP [5]
  475. Double-quotes allow white space and semi-colons to appear within
  476. a single argument.
  477. Command substitution, variable substitution, and backslash substitution
  478. still occur inside quotes.
  479. .IP [6]
  480. Braces defer interpretation of special characters.
  481. If a field begins with a left brace, then it consists of everything
  482. between the left brace and the matching right brace. The
  483. braces themselves are not included in the argument.
  484. No further processing is done on the information between the braces
  485. except that backslash-newline sequences are eliminated.
  486. .IP [7]
  487. If a field doesn't begin with a brace then backslash,
  488. variable, and command substitution are done on the field.  Only a
  489. single level of processing is done:  the results of one substitution
  490. are not scanned again for further substitutions or any other
  491. special treatment.  Substitution can
  492. occur on any field of a command, including the command name
  493. as well as the arguments.
  494. .IP [8]
  495. If the first non-blank character of a command is a \fB#\fR, everything
  496. from the \fB#\fR up through the next newline is treated as a comment
  497. and ignored.
  498.  
  499. .SH "EXPRESSIONS"
  500. .VS
  501. .PP
  502. The second major interpretation applied to strings in Tcl is
  503. as expressions.  Several commands, such as \fBexpr\fR, \fBfor\fR,
  504. and \fBif\fR, treat one or more of their arguments as expressions
  505. and call the Tcl expression processors (\fBTcl_ExprLong\fR,
  506. \fBTcl_ExprBoolean\fR, etc.) to evaluate them.
  507. The operators permitted in Tcl expressions are a subset of
  508. the operators permitted in C expressions, and they have the
  509. same meaning and precedence as the corresponding C operators.
  510. Expressions almost always yield numeric results
  511. (integer or floating-point values).
  512. For example, the expression
  513. .DS
  514. \fB8.2 + 6\fR
  515. .DE
  516. evaluates to 14.2.
  517. Tcl expressions differ from C expressions in the way that
  518. operands are specified, and in that Tcl expressions support
  519. non-numeric operands and string comparisons.
  520. .PP
  521. A Tcl expression consists of a combination of operands, operators,
  522. and parentheses.
  523. White space may be used between the operands and operators and
  524. parentheses; it is ignored by the expression processor.
  525. Where possible, operands are interpreted as integer values.
  526. Integer values may be specified in decimal (the normal case), in octal (if the
  527. first character of the operand is \fB0\fR), or in hexadecimal (if the first
  528. two characters of the operand are \fB0x\fR).
  529. If an operand does not have one of the integer formats given
  530. above, then it is treated as a floating-point number if that is
  531. possible.  Floating-point numbers may be specified in any of the
  532. ways accepted by an ANSI-compliant C compiler (except that the
  533. ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in
  534. most installations).  For example, all of the
  535. following are valid floating-point numbers:  2.1, 3., 6e4, 7.91e+16.
  536. If no numeric interpretation is possible, then an operand is left
  537. as a string (and only a limited set of operators may be applied to
  538. it).
  539. .PP
  540. Operands may be specified in any of the following ways:
  541. .IP [1]
  542. As an numeric value, either integer or floating-point.
  543. .IP [2]
  544. As a Tcl variable, using standard \fB$\fR notation.
  545. The variable's value will be used as the operand.
  546. .IP [3]
  547. As a string enclosed in double-quotes.
  548. The expression parser will perform backslash, variable, and
  549. command substitutions on the information between the quotes,
  550. and use the resulting value as the operand
  551. .IP [4]
  552. As a string enclosed in braces.
  553. The characters between the open brace and matching close brace
  554. will be used as the operand without any substitutions.
  555. .IP [5]
  556. As a Tcl command enclosed in brackets.
  557. The command will be executed and its result will be used as
  558. the operand.
  559. .LP
  560. Where substitutions occur above (e.g. inside quoted strings), they
  561. are performed by the expression processor.
  562. However, an additional layer of substitution may already have
  563. been performed by the command parser before the expression
  564. processor was called.
  565. As discussed below, it is usually best to enclose expressions
  566. in braces to prevent the command parser from performing substitutions
  567. on the contents.
  568. .PP
  569. For some examples of simple expressions, suppose the variable
  570. \fBa\fR has the value 3 and
  571. the variable \fBb\fR has the value 6.
  572. Then the expression on the left side of each of the lines below
  573. will evaluate to the value on the right side of the line:
  574. .DS
  575. .ta 6c
  576. \fB3.1 + $a    6.1
  577. 2 + "$a.$b"    5.6
  578. 4*[llength "6 2"]    8
  579. {word one} < "word $a"    0\fR
  580. .DE
  581. .PP
  582. The valid operators are listed below, grouped in decreasing order
  583. of precedence:
  584. .TP 20
  585. \fB\-\0\0~\0\0!\fR
  586. Unary minus, bit-wise NOT, logical NOT.  None of these operands
  587. may be applied to string operands, and bit-wise NOT may be
  588. applied only to integers.
  589. .TP 20
  590. \fB*\0\0/\0\0%\fR
  591. Multiply, divide, remainder.  None of these operands may be
  592. applied to string operands, and remainder may be applied only
  593. to integers.
  594. .TP 20
  595. \fB+\0\0\-\fR
  596. Add and subtract.  Valid for any numeric operands.
  597. .TP 20
  598. \fB<<\0\0>>\fR
  599. Left and right shift.  Valid for integer operands only.
  600. .TP 20
  601. \fB<\0\0>\0\0<=\0\0>=\fR
  602. Boolean less, greater, less than or equal, and greater than or equal.
  603. Each operator produces 1 if the condition is true, 0 otherwise.
  604. These operators may be applied to strings as well as numeric operands,
  605. in which case string comparison is used.
  606. .TP 20
  607. \fB==\0\0!=\fR
  608. Boolean equal and not equal.  Each operator produces a zero/one result.
  609. Valid for all operand types.
  610. .TP 20
  611. \fB&\fR
  612. Bit-wise AND.  Valid for integer operands only.
  613. .TP 20
  614. \fB^\fR
  615. Bit-wise exclusive OR.  Valid for integer operands only.
  616. .TP 20
  617. \fB|\fR
  618. Bit-wise OR.  Valid for integer operands only.
  619. .TP 20
  620. \fB&&\fR
  621. Logical AND.  Produces a 1 result if both operands are non-zero, 0 otherwise.
  622. Valid for numeric operands only (integers or floating-point).
  623. .TP 20
  624. \fB||\fR
  625. Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
  626. Valid for numeric operands only (integers or floating-point).
  627. .TP 20
  628. \fIx\fB?\fIy\fB:\fIz\fR
  629. If-then-else, as in C.  If \fIx\fR
  630. evaluates to non-zero, then the result is the value of \fIy\fR.
  631. Otherwise the result is the value of \fIz\fR.
  632. The \fIx\fR operand must have a numeric value.
  633. .LP
  634. See the C manual for more details on the results
  635. produced by each operator.
  636. All of the binary operators group left-to-right within the same
  637. precedence level.  For example, the expression
  638. .DS
  639. \fB4*2 < 7\fR
  640. .DE
  641. evaluates to 0.
  642. .PP
  643. The \fB&&\fP, \fB||\fP, and \fB?:\fP operators have ``lazy
  644. evaluation'', just as in C, 
  645. which means that operands are not evaluated if they are
  646. not needed to determine the outcome.  For example, in
  647. .DS
  648. \fB$v ? [a] : [b]\fR
  649. .DE
  650. only one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,
  651. depending on the value of \fB$v\fP.
  652. .PP
  653. All internal computations involving integers are done with the C type
  654. \fIlong\fP, and all internal computations involving floating-point are
  655. done with the C type \fIdouble\fP.
  656. When converting a string to floating-point, exponent overflow is
  657. detected and results in a Tcl error.
  658. For conversion to integer from string, detection of overflow depends
  659. on the behavior of some routines in the local C library, so it should
  660. be regarded as unreliable.
  661. In any case, overflow and underflow are generally not detected
  662. reliably for intermediate results.
  663. .PP
  664. Conversion among internal representations for integer, floating-point,
  665. and string operands is done automatically as needed.
  666. For arithmetic computations, integers are used until some
  667. floating-point number is introduced, after which floating-point is used.
  668. For example,
  669. .DS
  670. \fB5 / 4\fR
  671. .DE
  672. yields the result 1, while
  673. .DS
  674. \fB5 / 4.0\fR
  675. \fB5 / ( [string length "abcd"] + 0.0 )
  676. .DE
  677. both yield the result 1.25.
  678. .PP
  679. String values may be used as operands of the comparison operators,
  680. although the expression evaluator tries to do comparisons as integer
  681. or floating-point when it can.
  682. If one of the operands of a comparison is a string and the other
  683. has a numeric value, the numeric operand is converted back to
  684. a string using the C \fIsprintf\fP format specifier
  685. \fB%d\fR for integers and \fB%g\fR for floating-point values.
  686. For example, the expressions
  687. .DS
  688. \fB"0x03" > "2"\fR
  689. \fB"0y" < "0x12"\fR
  690. .DE
  691. both evaluate to 1.  The first comparison is done using integer
  692. comparison, and the second is done using string comparison after
  693. the second operand is converted to the string ``18''.
  694. .VE
  695. .PP
  696. In general it is safest to enclose an expression in braces when
  697. entering it in a command:  otherwise, if the expression contains
  698. any white space then the Tcl interpreter will split it
  699. among several arguments.  For example, the command
  700. .DS C
  701. \fBexpr $a + $b\fR
  702. .DE
  703. results in three arguments being passed to \fBexpr\fR:  \fB$a\fR,
  704. \fB+\fR, and \fB$b\fR.  In addition, if the expression isn't in braces
  705. then the Tcl interpreter will perform variable and command substitution
  706. immediately (it will happen in the command parser rather than in
  707. the expression parser).  In many cases the expression is being
  708. passed to a command that will evaluate the expression later (or
  709. even many times if, for example, the expression is to be used to
  710. decide when to exit a loop).  Usually the desired goal is to re-do
  711. the variable or command substitutions each time the expression is
  712. evaluated, rather than once and for all at the beginning.  For example,
  713. the command
  714. .DS C
  715. .ta 7c
  716. \fBfor {set i 1} $i<=10 {incr i} {...}\fR    *** WRONG ***
  717. .DE
  718. is probably intended to iterate over all values of \fBi\fR from 1 to 10.
  719. After each iteration of the body of the loop, \fBfor\fR will pass
  720. its second argument to the expression evaluator to see whether or not
  721. to continue processing.  Unfortunately, in this case the value of \fBi\fR
  722. in the second argument will be substituted once and for all when the
  723. \fBfor\fR command is parsed.  If \fBi\fR was 0 before the \fBfor\fR
  724. command was invoked then \fBfor\fR's second argument will be \fB0<=10\fR
  725. which will always evaluate to 1, even though \fBi\fR's value eventually
  726. becomes greater than 10.  In the above case the loop will never
  727. terminate.  Instead, the expression should be placed in braces:
  728. .DS C
  729. .ta 7c
  730. \fBfor {set i 1} {$i<=10} {incr i} {...}\fR    *** RIGHT ***
  731. .DE
  732. This causes the substitution of \fBi\fR's
  733. value to be delayed; it will be re-done each time the expression is
  734. evaluated, which is the desired result.
  735.  
  736. .SH LISTS
  737. .PP
  738. The third major way that strings are interpreted in Tcl is as lists.
  739. A list is just a string with a list-like structure
  740. consisting of fields separated by white space.  For example, the
  741. string
  742. .DS
  743. \fBAl Sue Anne John\fR
  744. .DE
  745. is a list with four elements or fields.
  746. Lists have the same basic structure as command strings, except
  747. that a newline character in a list is treated as a field separator
  748. just like space or tab.  Conventions for braces and quotes
  749. and backslashes are the same for lists as for commands.  For example,
  750. the string
  751. .DS
  752. \fBa b\e c {d e {f g h}}\fR
  753. .DE
  754. is a list with three elements:  \fBa\fR, \fBb c\fR, and \fBd e {f g h}\fR.
  755. Whenever an element
  756. is extracted from a list, the same rules about braces and quotes and
  757. backslashes are applied as for commands.  Thus in the example above
  758. when the third element is extracted from the list, the result is
  759. .DS
  760. \fBd e {f g h}\fR
  761. .DE
  762. (when the field was extracted, all that happened was to strip off
  763. the outermost layer of braces).  Command substitution and
  764. variable substitution are never
  765. made on a list (at least, not by the list-processing commands; the
  766. list can always be passed to the Tcl interpreter for evaluation).
  767. .PP
  768. The Tcl commands \fBconcat\fR, \fBforeach\fR, 
  769. .VS
  770. \fBlappend\fR, \fBlindex\fR, \fBlinsert\fR, \fBlist\fR, \fBllength\fR,
  771. \fBlrange\fR, \fBlreplace\fR, \fBlsearch\fR, and \fBlsort\fR allow
  772. you to build lists,
  773. .VE
  774. extract elements from them, search them, and perform other list-related
  775. functions.
  776.  
  777. .SH "REGULAR EXPRESSIONS"
  778. .VS
  779. .PP
  780. Tcl provides two commands that support string matching using
  781. \fBegrep\fR-style regular expressions: \fBregexp\fR and \fBregsub\fR.
  782. Regular expressions are implemented using Henry Spencer's package,
  783. and the description of regular expressions below is copied verbatim
  784. from his manual entry.
  785. .PP
  786. A regular expression is zero or more \fIbranches\fR, separated by ``|''.
  787. It matches anything that matches one of the branches.
  788. .PP
  789. A branch is zero or more \fIpieces\fR, concatenated.
  790. It matches a match for the first, followed by a match for the second, etc.
  791. .PP
  792. A piece is an \fIatom\fR possibly followed by ``*'', ``+'', or ``?''.
  793. An atom followed by ``*'' matches a sequence of 0 or more matches of the atom.
  794. An atom followed by ``+'' matches a sequence of 1 or more matches of the atom.
  795. An atom followed by ``?'' matches a match of the atom, or the null string.
  796. .PP
  797. An atom is a regular expression in parentheses (matching a match for the
  798. regular expression), a \fIrange\fR (see below), ``.''
  799. (matching any single character), ``^'' (matching the null string at the
  800. beginning of the input string), ``$'' (matching the null string at the
  801. end of the input string), a ``\e'' followed by a single character (matching
  802. that character), or a single character with no other significance
  803. (matching that character).
  804. .PP
  805. A \fIrange\fR is a sequence of characters enclosed in ``[]''.
  806. It normally matches any single character from the sequence.
  807. If the sequence begins with ``^'',
  808. it matches any single character \fInot\fR from the rest of the sequence.
  809. If two characters in the sequence are separated by ``\-'', this is shorthand
  810. for the full list of ASCII characters between them
  811. (e.g. ``[0-9]'' matches any decimal digit).
  812. To include a literal ``]'' in the sequence, make it the first character
  813. (following a possible ``^'').
  814. To include a literal ``\-'', make it the first or last character.
  815. .PP
  816. If a regular expression could match two different parts of a string,
  817. it will match the one which begins earliest.
  818. If both begin in the same place but match different lengths, or match
  819. the same length in different ways, life gets messier, as follows.
  820. .PP
  821. In general, the possibilities in a list of branches are considered in
  822. left-to-right order, the possibilities for ``*'', ``+'', and ``?'' are
  823. considered longest-first, nested constructs are considered from the
  824. outermost in, and concatenated constructs are considered leftmost-first.
  825. The match that will be chosen is the one that uses the earliest
  826. possibility in the first choice that has to be made.
  827. If there is more than one choice, the next will be made in the same manner
  828. (earliest possibility) subject to the decision on the first choice.
  829. And so forth.
  830. .PP
  831. For example, ``(ab|a)b*c'' could match ``abc'' in one of two ways.
  832. The first choice is between ``ab'' and ``a''; since ``ab'' is earlier, and does
  833. lead to a successful overall match, it is chosen.
  834. Since the ``b'' is already spoken for,
  835. the ``b*'' must match its last possibility\(emthe empty string\(emsince
  836. it must respect the earlier choice.
  837. .PP
  838. In the particular case where no ``|''s are present and there is only one
  839. ``*'', ``+'', or ``?'', the net effect is that the longest possible
  840. match will be chosen.
  841. So ``ab*'', presented with ``xabbbby'', will match ``abbbb''.
  842. Note that if ``ab*'' is tried against ``xabyabbbz'', it
  843. will match ``ab'' just after ``x'', due to the begins-earliest rule.
  844. (In effect, the decision on where to start the match is the first choice
  845. to be made, hence subsequent choices must respect it even if this leads them
  846. to less-preferred alternatives.)
  847. .VE
  848.  
  849. .SH "COMMAND RESULTS"
  850. .PP
  851. Each command produces two results:  a code and a string.  The
  852. code indicates whether the command completed successfully or not,
  853. and the string gives additional information.  The valid codes are
  854. defined in tcl.h, and are:
  855. .RS
  856. .TP 20
  857. \fBTCL_OK\fR
  858. This is the normal return code, and indicates that the command completed
  859. successfully.  The string gives the command's return value.
  860. .TP 20
  861. \fBTCL_ERROR\fR
  862. Indicates that an error occurred; the string gives a message describing
  863. the error.
  864. .VS
  865. In addition, the global variable \fBerrorInfo\fR will contain
  866. human-readable information
  867. describing which commands and procedures were being executed when the
  868. error occurred, and the global variable \fBerrorCode\fR will contain
  869. machine-readable details about the error, if they are available.
  870. See the section BUILT-IN VARIABLES below for more information.
  871. .VE
  872. .VE
  873. .TP 20
  874. \fBTCL_RETURN\fR
  875. Indicates that the \fBreturn\fR command has been invoked, and that the
  876. current procedure (or top-level command or \fBsource\fR command)
  877. should return immediately.  The
  878. string gives the return value for the procedure or command.
  879. .TP 20
  880. \fBTCL_BREAK\fR
  881. Indicates that the \fBbreak\fR command has been invoked, so the
  882. innermost loop should abort immediately.  The string should always
  883. be empty.
  884. .TP 20
  885. \fBTCL_CONTINUE\fR
  886. Indicates that the \fBcontinue\fR command has been invoked, so the
  887. innermost loop should go on to the next iteration.  The string
  888. should always be empty.
  889. .RE
  890. Tcl programmers do not normally need to think about return codes,
  891. since TCL_OK is almost always returned.  If anything else is returned
  892. by a command, then the Tcl interpreter immediately stops processing
  893. commands and returns to its caller.  If there are several nested
  894. invocations of the Tcl interpreter in progress, then each nested
  895. command will usually return the error to its caller, until eventually
  896. the error is reported to the top-level application code.  The
  897. application will then display the error message for the user.
  898. .PP
  899. In a few cases, some commands will handle certain ``error'' conditions
  900. themselves and not return them upwards.  For example, the \fBfor\fR
  901. command checks for the TCL_BREAK code; if it occurs, then \fBfor\fR
  902. stops executing the body of the loop and returns TCL_OK to its
  903. caller.  The \fBfor\fR command also handles TCL_CONTINUE codes and the
  904. procedure interpreter handles TCL_RETURN codes.  The \fBcatch\fR
  905. command allows Tcl programs to catch errors and handle them without
  906. aborting command interpretation any further.
  907.  
  908. .SH PROCEDURES
  909. .PP
  910. Tcl allows you to extend the command interface by defining
  911. procedures.  A Tcl procedure can be invoked just like any other Tcl
  912. command (it has a name and it receives one or more arguments).
  913. The only difference is that its body isn't a piece of C code linked
  914. into the program; it is a string containing one or more other
  915. Tcl commands.  See the \fBproc\fR command for information on
  916. how to define procedures and what happens when they are invoked.
  917.  
  918. .SH VARIABLES \- SCALARS AND ARRAYS
  919. .VS
  920. .PP
  921. Tcl allows the definition of variables and the use of their values
  922. either through \fB$\fR-style variable substitution, the \fBset\fR
  923. command, or a few other mechanisms.
  924. Variables need not be declared:  a new variable will automatically
  925. be created each time a new variable name is used.
  926. .PP
  927. Tcl supports two types of variables:  scalars and arrays.
  928. A scalar variable has a single value, whereas an array variable
  929. can have any number of elements, each with a name (called
  930. its ``index'') and a value.
  931. Array indexes may be arbitrary strings; they need not be numeric.
  932. Parentheses are used refer to array elements in Tcl commands.
  933. For example, the command
  934. .DS C
  935. \fBset x(first) 44\fR
  936. .DE
  937. will modify the element of \fBx\fR whose index is \fBfirst\fR
  938. so that its new value is \fB44\fR.
  939. Two-dimensional arrays can be simulated in Tcl by using indexes
  940. that contain multiple concatenated values.
  941. For example, the commands
  942. .DS C
  943. \fBset a(2,3) 1\fR
  944. \fBset a(3,6) 2\fR
  945. .DE
  946. set the elements of \fBa\fR whose indexes are \fB2,3\fR and \fB3,6\fR.
  947. .PP
  948. In general, array elements may be used anywhere in Tcl that scalar
  949. variables may be used.
  950. If an array is defined with a particular name, then there may
  951. not be a scalar variable with the same name.
  952. Similarly, if there is a scalar variable with a particular
  953. name then it is not possible to make array references to the
  954. variable.
  955. To convert a scalar variable to an array or vice versa, remove
  956. the existing variable with the \fBunset\fR command.
  957. .PP
  958. The \fBarray\fR command provides several features for dealing
  959. with arrays, such as querying the names of all the elements of
  960. the array and searching through the array one element at a time.
  961. .VE
  962. .PP
  963. Variables may be either global or local.  If a variable
  964. name is used when a procedure isn't being executed, then it
  965. automatically refers to a global variable.  Variable names used
  966. within a procedure normally refer to local variables associated with that
  967. invocation of the procedure.  Local variables are deleted whenever
  968. a procedure exits.  The \fBglobal\fR command may be used to request
  969. that a name refer to a global variable for the duration of the current
  970. procedure (this is somewhat analogous to \fBextern\fR in C).
  971.  
  972. .SH "BUILT-IN COMMANDS"
  973. .PP
  974. The Tcl library provides the following built-in commands, which will
  975. be available in any application using Tcl.  In addition to these
  976. built-in commands, there may be additional commands defined by each
  977. application, plus commands defined as Tcl procedures.
  978. In the command syntax descriptions below, words in boldface are
  979. literals that you type verbatim to Tcl.
  980. Words in italics are meta-symbols; they serve as names for any of
  981. a range of values that you can type.
  982. Optional arguments or groups of arguments are indicated by enclosing them
  983. in question-marks.
  984. Ellipses (``...'') indicate that any number of additional
  985. arguments or groups of arguments may appear, in the same format
  986. as the preceding argument(s).
  987. .TP
  988. \fBappend \fIvarName value \fR?\fIvalue value ...\fR?
  989. .VS
  990. Append all of the \fIvalue\fR arguments to the current value
  991. of variable \fIvarName\fR.  If \fIvarName\fR doesn't exist,
  992. it is given a value equal to the concatenation of all the
  993. \fIvalue\fR arguments.
  994. This command provides an efficient way to build up long
  995. variables incrementally.
  996. For example, ``\fBappend a $b\fR'' is much more efficient than
  997. ``\fBset a $a$b\fR'' if \fB$a\fR is long.
  998. .VE
  999. .TP
  1000. \fBarray \fIoption arrayName\fR ?\fIarg arg ...\fR?
  1001. .VS
  1002. This command performs one of several operations on the
  1003. variable given by \fIarrayName\fR.
  1004. \fIArrayName\fR must be the name of an existing array variable.
  1005. The \fIoption\fR argument determines what action is carried
  1006. out by the command.
  1007. The legal \fIoptions\fR (which may be abbreviated) are:
  1008. .RS
  1009. .TP
  1010. \fBarray anymore \fIarrayName searchId\fR
  1011. Returns 1 if there are any more elements left to be processed
  1012. in an array search, 0 if all elements have already been
  1013. returned.
  1014. \fISearchId\fR indicates which search on \fIarrayName\fR to
  1015. check, and must have been the return value from a previous
  1016. invocation of \fBarray startsearch\fR.
  1017. This option is particularly useful if an array has an element
  1018. with an empty name, since the return value from
  1019. \fBarray nextelement\fR won't indicate whether the search
  1020. has been completed.
  1021. .TP
  1022. \fBarray donesearch \fIarrayName searchId\fR
  1023. This command terminates an array search and destroys all the
  1024. state associated with that search.  \fISearchId\fR indicates
  1025. which search on \fIarrayName\fR to destroy, and must have
  1026. been the return value from a previous invocation of
  1027. \fBarray startsearch\fR.  Returns an empty string.
  1028. .TP
  1029. \fBarray names \fIarrayName\fR
  1030. Returns a list containing the names of all of the elements in
  1031. the array.
  1032. If there are no elements in the array then an empty string is
  1033. returned.
  1034. .TP
  1035. \fBarray nextelement \fIarrayName searchId\fR
  1036. Returns the name of the next element in \fIarrayName\fR, or
  1037. an empty string if all elements of \fIarrayName\fR have
  1038. already been returned in this search.  The \fIsearchId\fR
  1039. argument identifies the search, and must have
  1040. been the return value of an \fBarray startsearch\fR command.
  1041. Warning:  if elements are added to or deleted from the array,
  1042. then all searches are automatically terminated just as if
  1043. \fBarray donesearch\fR had been invoked; this will cause
  1044. \fBarray nextelement\fR operations to fail for those searches.
  1045. .TP
  1046. \fBarray size \fIarrayName\fR
  1047. Returns a decimal string giving the number of elements in the
  1048. array.
  1049. .TP
  1050. \fBarray startsearch \fIarrayName\fR
  1051. This command initializes an element-by-element search through the
  1052. array given by \fIarrayName\fR, such that invocations of the
  1053. \fBarray nextelement\fR command will return the names of the
  1054. individual elements in the array.
  1055. When the search has been completed, the \fBarray donesearch\fR
  1056. command should be invoked.
  1057. The return value is a
  1058. search identifier that must be used in \fBarray nextelement\fR
  1059. and \fBarray donesearch\fR commands; it allows multiple
  1060. searches to be underway simultaneously for the same array.
  1061. .VE
  1062. .RE
  1063. .TP
  1064. \fBbreak\fR
  1065. This command may be invoked only inside the body of a loop command
  1066. such as \fBfor\fR or \fBforeach\fR or \fBwhile\fR.  It returns a TCL_BREAK code
  1067. to signal the innermost containing loop command to return immediately.
  1068. .TP
  1069. \fBcase\fI string \fR?\fBin\fR? \fIpatList body \fR?\fIpatList body \fR...?
  1070. .TP
  1071. \fBcase\fI string \fR?\fBin\fR? {\fIpatList body \fR?\fIpatList body \fR...?}
  1072. Match \fIstring\fR against each of the \fIpatList\fR arguments
  1073. in order.  If one matches, then evaluate the following \fIbody\fR argument
  1074. by passing it recursively to the Tcl interpreter, and return the result
  1075. of that evaluation.  Each \fIpatList\fR argument consists of a single
  1076. pattern or list of patterns.  Each pattern may contain any of the wild-cards
  1077. described under \fBstring match\fR.  If a \fIpatList\fR
  1078. argument is \fBdefault\fR, the corresponding body will be evaluated
  1079. if no \fIpatList\fR matches \fIstring\fR.  If no \fIpatList\fR argument
  1080. matches \fIstring\fR and no default is given, then the \fBcase\fR
  1081. command returns an empty string.
  1082. .RS
  1083. .PP
  1084. Two syntaxes are provided.
  1085. The first uses a separate argument for each of the patterns and commands;
  1086. this form is convenient if substitutions are desired on some of the
  1087. patterns or commands.
  1088. .VS
  1089. The second form places all of the patterns and commands together into
  1090. a single argument; the argument must have proper list structure, with
  1091. the elements of the list being the patterns and commands.
  1092. The second form makes it easy to construct multi-line case commands,
  1093. since the braces around the whole list make it unnecessary to include a
  1094. backslash at the end of each line.
  1095. Since the \fIpatList\fR arguments are in braces in the second form,
  1096. no command or variable substitutions are performed on them;  this makes
  1097. the behavior of the second form different than the first form in some
  1098. cases.
  1099. .PP
  1100. Below are some examples of \fBcase\fR commands:
  1101. .DS
  1102. \fBcase abc in {a b} {format 1} default {format 2} a* {format 3}
  1103. .DE
  1104. will return \fB3\fR, 
  1105. .DS
  1106. .ta .5c 1c
  1107. \fBcase a in {
  1108.     {a b} {format 1}
  1109.     default {format 2}
  1110.     a* {format 3}
  1111. }
  1112. .DE
  1113. will return \fB1\fR, and
  1114. .DS
  1115. .ta .5c 1c
  1116. \fBcase xyz {
  1117.     {a b}
  1118.         {format 1}
  1119.     default
  1120.         {format 2}
  1121.     a*
  1122.         {format 3}
  1123. }
  1124. .DE
  1125. will return \fB2\fR.
  1126. .VE
  1127. .RE
  1128. .TP
  1129. \fBcatch\fI command \fR?\fIvarName\fR?
  1130. The \fBcatch\fR command may be used to prevent errors from aborting
  1131. command interpretation.  \fBCatch\fR calls the Tcl interpreter recursively
  1132. to execute \fIcommand\fR, and always returns a TCL_OK code, regardless of
  1133. any errors that might occur while executing \fIcommand\fR.  The return
  1134. value from \fBcatch\fR is a decimal string giving the
  1135. code returned by the Tcl interpreter after executing \fIcommand\fR.
  1136. This will be \fB0\fR (TCL_OK) if there were no errors in \fIcommand\fR; otherwise
  1137. it will have a non-zero value corresponding to one of the exceptional
  1138. return codes (see tcl.h for the definitions of code values).  If the
  1139. \fIvarName\fR argument is given, then it gives the name of a variable;
  1140. \fBcatch\fR will set the value of the variable to the string returned
  1141. from \fIcommand\fR (either a result or an error message).
  1142. .TP
  1143. \fBcd \fR?\fIdirName\fR?
  1144. .VS
  1145. Change the current working directory to \fIdirName\fR, or to the
  1146. home directory (as specified in the HOME environment variable) if
  1147. \fIdirName\fR is not given.
  1148. If \fIdirName\fR starts with a tilde, then tilde-expansion is
  1149. done as described for \fBTcl_TildeSubst\fR.
  1150. Returns an empty string.
  1151. This command can potentially be disruptive to an application,
  1152. so it may be removed in some applications.
  1153. .TP
  1154. \fBclose \fIfileId\fR
  1155. Closes the file given by \fIfileId\fR.
  1156. \fIFileId\fR must be the return value from a previous invocation
  1157. of the \fBopen\fR command; after this command, it should not be
  1158. used anymore.
  1159. If \fIfileId\fR refers to a command pipeline instead of a file,
  1160. then \fBclose\fR waits for the children to complete.
  1161. The normal result of this command is an empty string, but errors
  1162. are returned if there are problems in closing the file or waiting
  1163. for children to complete.
  1164. .VE
  1165. .TP
  1166. \fBconcat\fI arg \fR?\fIarg ...\fR?
  1167. This command treats each argument as a list and concatenates them
  1168. into a single list.  It permits any number of arguments.  For example,
  1169. the command
  1170. .RS
  1171. .DS
  1172. \fBconcat a b {c d e} {f {g h}}\fR
  1173. .DE
  1174. will return
  1175. .DS
  1176. \fBa b c d e f {g h}\fR
  1177. .DE
  1178. as its result.
  1179. .RE
  1180. .TP
  1181. \fBcontinue\fR
  1182. This command may be invoked only inside the body of a loop command
  1183. such as \fBfor\fR or \fBforeach\fR or \fBwhile\fR.  It
  1184. returns a  TCL_CONTINUE code
  1185. to signal the innermost containing loop command to skip the
  1186. remainder of the loop's body
  1187. but continue with the next iteration of the loop.
  1188. .TP
  1189. \fBeof \fIfileId\fR
  1190. .VS
  1191. Returns 1 if an end-of-file condition has occurred on \fIfileId\fR,
  1192. 0 otherwise.
  1193. \fIFileId\fR must have been the return
  1194. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  1195. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  1196. channels.
  1197. .VE
  1198. .TP
  1199. \fBerror \fImessage\fR ?\fIinfo\fR? ?\fIcode\fR?
  1200. Returns a TCL_ERROR code, which causes command interpretation to be
  1201. unwound.  \fIMessage\fR is a string that is returned to the application
  1202. to indicate what went wrong.
  1203. .RS
  1204. .PP
  1205. If the \fIinfo\fR argument is provided and is non-empty,
  1206. it is used to initialize the global variable \fBerrorInfo\fR.
  1207. \fBerrorInfo\fR is used to accumulate a stack trace of what
  1208. was in progress when an error occurred; as nested commands unwind,
  1209. the Tcl interpreter adds information to \fBerrorInfo\fR.  If the
  1210. \fIinfo\fR argument is present, it is used to initialize
  1211. \fBerrorInfo\fR and the first increment of unwind information
  1212. will not be added by the Tcl interpreter.  In other
  1213. words, the command containing the \fBerror\fR command will not appear
  1214. in \fBerrorInfo\fR; in its place will be \fIinfo\fR.
  1215. This feature is most useful in conjunction with the \fBcatch\fR command:
  1216. if a caught error cannot be handled successfully, \fIinfo\fR can be used
  1217. to return a stack trace reflecting the original point of occurrence
  1218. of the error:
  1219. .DS
  1220. \fBcatch {...} errMsg
  1221. set savedInfo $errorInfo
  1222. \&...
  1223. error $errMsg $savedInfo\fR
  1224. .DE
  1225. .PP
  1226. .VS
  1227. If the \fIcode\fR argument is present, then its value is stored
  1228. in the \fBerrorCode\fR global variable.  This variable is intended
  1229. to hold a machine-readable description of the error in cases where
  1230. such information is available; see the section BUILT-IN VARIABLES
  1231. below for information on the proper format for the variable.
  1232. If the \fIcode\fR argument is not
  1233. present, then \fBerrorCode\fR is automatically reset to
  1234. ``NONE'' by the Tcl interpreter as part of processing the
  1235. error generated by the command.
  1236. .VE
  1237. .RE
  1238. .TP
  1239. \fBeval \fIarg \fR?\fIarg ...\fR?
  1240. \fBEval\fR takes one or more arguments, which together comprise a Tcl
  1241. command (or collection of Tcl commands separated by newlines in the
  1242. usual way).  \fBEval\fR concatenates all its arguments in the same
  1243. fashion as the \fBconcat\fR command, passes the concatenated string to the
  1244. Tcl interpreter recursively, and returns the result of that
  1245. evaluation (or any error generated by it).
  1246. .TP
  1247. \fBexec \fIarg \fR?\fIarg ...\fR?
  1248. .VS
  1249. This command treats its arguments as the specification
  1250. of one or more UNIX commands to execute as subprocesses.
  1251. The commands take the form of a standard shell pipeline;
  1252. ``|'' arguments separate commands in the
  1253. pipeline and cause standard output of the preceding command
  1254. to be piped into standard input of the next command.
  1255. .RS
  1256. .PP
  1257. Under normal conditions the result of the \fBexec\fR command
  1258. consists of the standard output produced by the last command
  1259. in the pipeline.
  1260. If any of the commands in the pipeline exit abnormally or
  1261. are killed or suspended, then \fBexec\fR will return an error
  1262. and the error message will include the pipeline's output followed by
  1263. error messages describing the abnormal terminations; the
  1264. \fBerrorCode\fR variable will contain additional information
  1265. about the last abnormal termination encountered.
  1266. If any of the commands writes to its standard error file,
  1267. then \fBexec\fR will return an error, and the error message
  1268. will include the pipeline's output, followed by messages
  1269. about abnormal terminations (if any), followed by the standard error
  1270. output.
  1271. .PP
  1272. If the last character of the result or error message
  1273. is a newline then that character is deleted from the result
  1274. or error message for consistency with normal
  1275. Tcl return values.
  1276. .PP
  1277. If an \fIarg\fR has the value ``>'' then the
  1278. following argument is taken as the name of a file and
  1279. the standard output of the last command in the pipeline
  1280. is redirected to the file.  In this situation \fBexec\fR
  1281. will normally return an empty string.
  1282. .PP
  1283. If an \fIarg\fR has the value ``<'' then the following
  1284. argument is taken as the name of a file to use
  1285. for standard input to the first command in the
  1286. pipeline.
  1287. If an argument has the value ``<<'' then the following
  1288. argument is taken as an immediate value to be passed to
  1289. the first command as standard input.
  1290. If there is no ``<'' or ``<<'' argument then the standard
  1291. input for the first command in the pipeline is taken from
  1292. the application's current standard input.
  1293. .PP
  1294. If the last \fIarg\fR is ``&'' then the command will be
  1295. executed in background.
  1296. In this case the standard output from the last command
  1297. in the pipeline will
  1298. go to the application's standard output unless
  1299. redirected in the command, and error output from all
  1300. the commands in the pipeline will go to the application's
  1301. standard error file.
  1302. .PP
  1303. Each \fIarg\fR becomes one word for a command, except for
  1304. ``|'', ``<'', ``<<'', ``>'', and ``&'' arguments, and the
  1305. arguments that follow ``<'', ``<<'', and ``>''.
  1306. The first word in each command is taken as the command name;
  1307. tilde-substitution is performed on it, and the directories
  1308. in the PATH environment variable are searched for
  1309. an executable by the given name.
  1310. No ``glob'' expansion or other shell-like substitutions
  1311. are performed on the arguments to commands.
  1312. .RE
  1313. .TP
  1314. \fBexit \fR?returnCode\fR?
  1315. Terminate the process, returning \fIreturnCode\fR to the
  1316. parent as the exit status.
  1317. If \fIreturnCode\fR isn't specified then it defaults
  1318. to 0.
  1319. .VE
  1320. .TP
  1321. \fBexpr \fIarg\fR
  1322. Calls the expression processor to evaluate \fIarg\fR, and returns
  1323. the result as a string.  See the section EXPRESSIONS above.
  1324. .TP
  1325. \fBfile \fIoption\fR \fIname\fR ?\fIarg arg ...\fR?
  1326. .VS
  1327. Operate on a file or a file name.  \fIName\fR is the name of a file;
  1328. if it starts with a tilde, then tilde substitution is done before
  1329. executing the command (see the manual entry for \fBTcl_TildeSubst\fR
  1330. for details).
  1331. \fIOption\fR indicates what to do with the file name.  Any unique
  1332. abbreviation for \fIoption\fR is acceptable.  The valid options are:
  1333. .RS
  1334. .TP
  1335. \fBfile \fBatime \fIname\fR
  1336. Return a decimal string giving the time at which file \fIname\fR
  1337. was last accessed.  The time is measured in the standard UNIX
  1338. fashion as seconds from a fixed starting time (often January 1, 1970).
  1339. If the file doesn't exist or its access time cannot be queried then an
  1340. error is generated.
  1341. .TP
  1342. \fBfile \fBdirname \fIname\fR
  1343. Return all of the characters in \fIname\fR up to but not including
  1344. the last slash character.  If there are no slashes in \fIname\fR
  1345. then return ``.''.  If the last slash in \fIname\fR is its first
  1346. character, then return ``/''.
  1347. .TP
  1348. \fBfile \fBexecutable \fIname\fR
  1349. Return \fB1\fR if file \fIname\fR is executable by
  1350. the current user, \fB0\fR otherwise.
  1351. .TP
  1352. \fBfile \fBexists \fIname\fR
  1353. Return \fB1\fR if file \fIname\fR exists and the current user has
  1354. search privileges for the directories leading to it, \fB0\fR otherwise.
  1355. .TP
  1356. \fBfile \fBextension \fIname\fR
  1357. Return all of the characters in \fIname\fR after and including the
  1358. last dot in \fIname\fR.  If there is no dot in \fIname\fR then return
  1359. the empty string.
  1360. .TP
  1361. \fBfile \fBisdirectory \fIname\fR
  1362. Return \fB1\fR if file \fIname\fR is a directory,
  1363. \fB0\fR otherwise.
  1364. .TP
  1365. \fBfile \fBisfile \fIname\fR
  1366. Return \fB1\fR if file \fIname\fR is a regular file,
  1367. \fB0\fR otherwise.
  1368. .TP
  1369. \fBfile lstat \fIname varName\fR
  1370. Same as \fBstat\fR option (see below) except uses the \fIlstat\fR
  1371. kernel call instead of \fIstat\fR.  This means that if \fIname\fR
  1372. refers to a symbolic link the information returned in \fIvarName\fR
  1373. is for the link rather than the file it refers to.  On systems that
  1374. don't support symbolic links this option behaves exactly the same
  1375. as the \fBstat\fR option.
  1376. .TP
  1377. \fBfile \fBmtime \fIname\fR
  1378. Return a decimal string giving the time at which file \fIname\fR
  1379. was last modified.  The time is measured in the standard UNIX
  1380. fashion as seconds from a fixed starting time (often January 1, 1970).
  1381. If the file doesn't exist or its modified time cannot be queried then an
  1382. error is generated.
  1383. .TP
  1384. \fBfile \fBowned \fIname\fR
  1385. Return \fB1\fR if file \fIname\fR is owned by the current user,
  1386. \fB0\fR otherwise.
  1387. .TP
  1388. \fBfile \fBreadable \fIname\fR
  1389. Return \fB1\fR if file \fIname\fR is readable by
  1390. the current user, \fB0\fR otherwise.
  1391. .TP
  1392. \fBfile readlink \fIname\fR
  1393. Returns the value of the symbolic link given by \fIname\fR (i.e. the
  1394. name of the file it points to).  If
  1395. \fIname\fR isn't a symbolic link or its value cannot be read, then
  1396. an error is returned.  On systems that don't support symbolic links
  1397. this option is undefined.
  1398. .TP
  1399. \fBfile \fBrootname \fIname\fR
  1400. Return all of the characters in \fIname\fR up to but not including
  1401. the last ``.'' character in the name.  If \fIname\fR doesn't contain
  1402. a dot, then return \fIname\fR.
  1403. .TP
  1404. \fBfile \fBsize \fIname\fR
  1405. Return a decimal string giving the size of file \fIname\fR in bytes.
  1406. If the file doesn't exist or its size cannot be queried then an
  1407. error is generated.
  1408. .TP
  1409. \fBfile \fBstat  \fIname varName\fR
  1410. Invoke the \fBstat\fR kernel call on \fIname\fR, and use the
  1411. variable given by \fIvarName\fR to hold information returned from
  1412. the kernel call.
  1413. \fIVarName\fR is treated as an array variable,
  1414. and the following elements of that variable are set: \fBatime\fR,
  1415. \fBctime\fR, \fBdev\fR, \fBgid\fR, \fBino\fR, \fBmode\fR, \fBmtime\fR,
  1416. \fBnlink\fR, \fBsize\fR, \fBtype\fR, \fBuid\fR.
  1417. Each element except \fBtype\fR is a decimal string with the value of
  1418. the corresponding field from the \fBstat\fR return structure; see the
  1419. manual entry for \fBstat\fR for details on the meanings of the values.
  1420. The \fBtype\fR element gives the type of the file in the same form
  1421. returned by the command \fBfile type\fR.
  1422. This command returns an empty string.
  1423. .TP
  1424. \fBfile \fBtail \fIname\fR
  1425. Return all of the characters in \fIname\fR after the last slash.
  1426. If \fIname\fR contains no slashes then return \fIname\fR.
  1427. .TP
  1428. \fBfile \fBtype \fIname\fR
  1429. Returns a string giving the type of file \fIname\fR, which will be
  1430. one of \fBfile\fR, \fBdirectory\fR, \fBcharacterSpecial\fR,
  1431. \fBblockSpecial\fR, \fBfifo\fR, \fBlink\fR, or \fBsocket\fR.
  1432. .TP
  1433. \fBfile \fBwritable \fIname\fR
  1434. Return \fB1\fR if file \fIname\fR is writable by
  1435. the current user, \fB0\fR otherwise.
  1436. .RE
  1437. .IP
  1438. The \fBfile\fR commands that return 0/1 results are often used in
  1439. conditional or looping commands, for example:
  1440. .RS
  1441. .DS
  1442. \fBif {![file exists foo]} then {error {bad file name}} else {...}\fR
  1443. .DE
  1444. .VE
  1445. .RE
  1446. .TP
  1447. \fBflush \fIfileId\fR
  1448. .VS
  1449. Flushes any output that has been buffered for \fIfileId\fR.
  1450. \fIFileId\fR must have been the return
  1451. value from a previous call to \fBopen\fR, or it may be
  1452. \fBstdout\fR or \fBstderr\fR to access one of the standard I/O streams;
  1453. it must refer to a file that was opened for writing.
  1454. This command returns an empty string.
  1455. .VE
  1456. .TP
  1457. \fBfor \fIstart test next body\fR
  1458. \fBFor\fR is a looping command, similar in structure to the C
  1459. \fBfor\fR statement.  The \fIstart\fR, \fInext\fR, and
  1460. \fIbody\fR arguments must be Tcl command strings, and \fItest\fR
  1461. is an expression string.
  1462. The \fBfor\fR command first invokes the Tcl interpreter to
  1463. execute \fIstart\fR.  Then it repeatedly evaluates \fItest\fR as
  1464. an expression; if the result is non-zero it invokes the Tcl
  1465. interpreter on \fIbody\fR, then invokes the Tcl interpreter on \fInext\fR,
  1466. then repeats the loop.  The command terminates when \fItest\fR evaluates
  1467. to 0.  If a \fBcontinue\fR command is invoked within \fIbody\fR then
  1468. any remaining commands in the current execution of \fIbody\fR are skipped;
  1469. processing continues by invoking the Tcl interpreter on \fInext\fR, then
  1470. evaluating \fItest\fR, and so on.  If a \fBbreak\fR command is invoked
  1471. within \fIbody\fR
  1472. or \fInext\fR,
  1473. then the \fBfor\fR command will
  1474. return immediately.
  1475. The operation of \fBbreak\fR and \fBcontinue\fR are similar to the
  1476. corresponding statements in C.
  1477. \fBFor\fR returns an empty string.
  1478. .TP
  1479. \fBforeach \fIvarname list body\fR
  1480. In this command, \fIvarname\fR is the name of a variable, \fIlist\fR
  1481. is a list of values to assign to \fIvarname\fR, and \fIbody\fR is a
  1482. collection of Tcl commands.  For each field in \fIlist\fR (in order
  1483. from left to right), \fBforeach\fR assigns the contents of the
  1484. field to \fIvarname\fR (as if the \fBlindex\fR command had been used
  1485. to extract the field), then calls the Tcl interpreter to execute
  1486. \fIbody\fR.  The \fBbreak\fR and \fBcontinue\fR statements may be
  1487. invoked inside \fIbody\fR, with the same effect as in the \fBfor\fR
  1488. command.  \fBForeach\fR returns an empty string.
  1489. .TP
  1490. \fBformat \fIformatString \fR?\fIarg arg ...\fR?
  1491. This command generates a formatted string in the same way as the
  1492. C \fBsprintf\fR procedure (it uses \fBsprintf\fR in its
  1493. implementation).  \fIFormatString\fR indicates how to format
  1494. the result, using \fB%\fR fields as in \fBsprintf\fR, and the additional
  1495. arguments, if any, provide values to be substituted into the result.
  1496. All of the \fBsprintf\fR options are valid; see the \fBsprintf\fR
  1497. man page for details.  Each \fIarg\fR must match the expected type
  1498. from the \fB%\fR field in \fIformatString\fR; the \fBformat\fR command
  1499. converts each argument to the correct type (floating, integer, etc.)
  1500. before passing it to \fBsprintf\fR for formatting.
  1501. The only unusual conversion is for \fB%c\fR; in this case the argument
  1502. must be a decimal string, which will then be converted to the corresponding
  1503. ASCII character value.
  1504. \fBFormat\fR does backslash substitution on its \fIformatString\fR
  1505. argument, so backslash sequences in \fIformatString\fR will be handled
  1506. correctly even if the argument is in braces.
  1507. The return value from \fBformat\fR
  1508. is the formatted string.
  1509. .TP
  1510. \fBgets \fIfileId\fR ?\fIvarName\fR?
  1511. .VS
  1512. Reads the next line from the file given by \fIfileId\fR and discards
  1513. the terminating newline character.
  1514. If \fIvarName\fR is specified, then the line is placed in the variable
  1515. by that name and the return value is a count of the number of characters
  1516. read (not including the newline).
  1517. If the end of the file is reached before reading
  1518. any characters then \-1 is returned and \fIvarName\fR is set to an
  1519. empty string.
  1520. If \fIvarName\fR is not specified then the return value will be
  1521. the line (minus the newline character) or an empty string if
  1522. the end of the file is reached before reading any characters.
  1523. An empty string will also be returned if a line contains no characters
  1524. except the newline, so \fBeof\fR may have to be used to determine
  1525. what really happened.
  1526. If the last character in the file is not a newline character, then
  1527. \fBgets\fR behaves as if there were an additional newline character
  1528. at the end of the file.
  1529. \fIFileId\fR must be \fBstdin\fR or the return value from a previous
  1530. call to \fBopen\fR; it must refer to a file that was opened
  1531. for reading.
  1532. .VE
  1533. .TP
  1534. \fBglob \fR?\fB\-nocomplain\fR? \fIfilename\fR ?\fIfilename ...\fR?
  1535. This command performs filename globbing, using csh rules.  The returned
  1536. value from \fBglob\fR is the list of expanded filenames.
  1537. .VS
  1538. If \fB\-nocomplain\fR is specified as the first argument then an empty
  1539. list may be returned;  otherwise an error is returned if the expanded
  1540. list is empty.  The \fB\-nocomplain\fR argument must be provided
  1541. exactly: an abbreviation will not be accepted.
  1542. .VE
  1543. .TP
  1544. \fBglobal \fIvarname \fR?\fIvarname ...\fR?
  1545. This command is ignored unless a Tcl procedure is being interpreted.
  1546. If so, then it declares the given \fIvarname\fR's to be global variables
  1547. rather than local ones.  For the duration of the current procedure
  1548. (and only while executing in the current procedure), any reference to
  1549. any of the \fIvarname\fRs will be bound to a global variable instead
  1550. of a local one.
  1551. .TP
  1552. \fBhistory \fR?\fIoption\fR? ?\fIarg arg ...\fR?
  1553. Note:  this command may not be available in all Tcl-based applications.
  1554. Typically, only those that receive command input in a typescript
  1555. form will support history.
  1556. The \fBhistory\fR command performs one of several operations related to
  1557. recently-executed commands recorded in a history list.  Each of
  1558. these recorded commands is referred to as an ``event''.  When
  1559. specifying an event to the \fBhistory\fR command, the following
  1560. forms may be used:
  1561. .RS
  1562. .IP [1]
  1563. A number:  if positive, it refers to the event with
  1564. that number (all events are numbered starting at 1).  If the number
  1565. is negative, it selects an event relative to the current event
  1566. (\fB\-1\fR refers to the previous event, \fB\-2\fR to the one before that, and
  1567. so on).
  1568. .IP [2]
  1569. A string:  selects the most recent event that matches the string.
  1570. An event is considered to match the string either if the string is
  1571. the same as the first characters of the event, or if the string
  1572. matches the event in the sense of the \fBstring match\fR command.
  1573. .LP
  1574. The \fBhistory\fR command can take any of the following forms:
  1575. .TP
  1576. \fBhistory\fR
  1577. Same
  1578. .VS
  1579. as \fBhistory info\fR, described below.
  1580. .VE
  1581. .TP
  1582. \fBhistory add\fI command \fR?\fBexec\fR?
  1583. Add the \fIcommand\fR argument to the history list as a new event.  If
  1584. \fBexec\fR is specified (or abbreviated) then the command is also
  1585. executed and its result is returned.  If \fBexec\fR isn't specified
  1586. then an empty string is returned as result.
  1587. .TP
  1588. \fBhistory change\fI newValue\fR ?\fIevent\fR?
  1589. Replace the value recorded for an event with \fInewValue\fR.  \fIEvent\fR
  1590. specifies the event to replace, and
  1591. defaults to the \fIcurrent\fR event (not event \fB\-1\fR).  This command
  1592. is intended for use in commands that implement new forms of history
  1593. substitution and wish to replace the current event (which invokes the
  1594. substitution) with the command created through substitution.  The return
  1595. value is an empty string.
  1596. .TP
  1597. \fBhistory event\fR ?\fIevent\fR?
  1598. Returns the value of the event given by \fIevent\fR.  \fIEvent\fR
  1599. defaults to \fB\-1\fR.  This command causes history revision to occur:
  1600. see below for details.
  1601. .TP
  1602. \fBhistory info \fR?\fIcount\fR?
  1603. Returns a formatted string (intended for humans to read) giving
  1604. the event number and contents for each of the events in the history
  1605. list except the current event.  If \fIcount\fR is specified
  1606. then only the most recent \fIcount\fR events are returned.
  1607. .TP
  1608. \fBhistory keep \fIcount\fR
  1609. This command may be used to change the size of the history list to
  1610. \fIcount\fR events.  Initially, 20 events are retained in the history
  1611. list.  This command returns an empty string.
  1612. .TP
  1613. \fBhistory nextid\fR
  1614. Returns the number of the next event to be recorded
  1615. in the history list.  It is useful for things like printing the
  1616. event number in command-line prompts.
  1617. .TP
  1618. \fBhistory redo \fR?\fIevent\fR?
  1619. Re-execute the command indicated by \fIevent\fR and return its result.
  1620. \fIEvent\fR defaults to \fB\-1\fR.  This command results in history
  1621. revision:  see below for details.
  1622. .TP
  1623. \fBhistory substitute \fIold new \fR?\fIevent\fR?
  1624. Retrieve the command given by \fIevent\fR
  1625. (\fB\-1\fR by default), replace any occurrences of \fIold\fR by
  1626. \fInew\fR in the command (only simple character equality is supported;
  1627. no wild cards), execute the resulting command, and return the result
  1628. of that execution.  This command results in history
  1629. revision:  see below for details.
  1630. .TP
  1631. \fBhistory words \fIselector\fR ?\fIevent\fR?
  1632. Retrieve from the command given by \fIevent\fR (\fB\-1\fR by default)
  1633. the words given by \fIselector\fR, and return those words in a string
  1634. separated by spaces.  The \fBselector\fR argument has three forms.
  1635. If it is a single number then it selects the word given by that
  1636. number (\fB0\fR for the command name, \fB1\fR for its first argument,
  1637. and so on).  If it consists of two numbers separated by a dash,
  1638. then it selects all the arguments between those two.  Otherwise
  1639. \fBselector\fR is treated as a pattern; all words matching that
  1640. pattern (in the sense of \fBstring match\fR) are returned.  In
  1641. the numeric forms \fB$\fR may be used
  1642. to select the last word of a command.
  1643. For example, suppose the most recent command in the history list is
  1644. .RS
  1645. .DS
  1646. \fBformat  {%s is %d years old} Alice [expr $ageInMonths/12]\fR
  1647. .DE
  1648. Below are some history commands and the results they would produce:
  1649. .DS
  1650. .ta 4c
  1651. .fi
  1652. .UL Command "    "
  1653. .UL Result
  1654. .nf
  1655.  
  1656. \fBhistory words $    [expr $ageInMonths/12]\fR
  1657. \fBhistory words 1-2    {%s is %d years  old} Alice\fR
  1658. \fBhistory words *a*o*    {%s is %d years old} [expr $ageInMonths/12]\fR
  1659. .DE
  1660. \fBHistory words\fR results in history revision:  see below for details.
  1661. .RE
  1662. The history options \fBevent\fR, \fBredo\fR, \fBsubstitute\fR,
  1663. and \fBwords\fR result in ``history revision''.
  1664. When one of these options is invoked then the current event
  1665. is modified to eliminate the history command and replace it with
  1666. the result of the history command.
  1667. For example, suppose that the most recent command in the history
  1668. list is
  1669. .DS
  1670. \fBset a [expr $b+2]\fR
  1671. .DE
  1672. and suppose that the next command invoked is one of the ones on
  1673. the left side of the table below.  The command actually recorded in
  1674. the history event will be the corresponding one on the right side
  1675. of the table.
  1676. .ne 1.5c
  1677. .DS
  1678. .ta 4c
  1679. .fi
  1680. .UL "Command Typed" "    "
  1681. .UL "Command Recorded"
  1682. .nf
  1683.  
  1684. \fBhistory redo    set a [expr $b+2]\fR
  1685. \fBhistory s a b    set b [expr $b+2]\fR
  1686. \fBset c [history w 2]    set c [expr $b+2]\fR
  1687. .DE
  1688. .VS
  1689. History revision is needed because event specifiers like \fB\-1\fR
  1690. are only valid at a particular time:  once more events have been
  1691. added to the history list a different event specifier would be
  1692. needed.
  1693. History revision occurs even when \fBhistory\fR is invoked
  1694. indirectly from the current event (e.g. a user types a command
  1695. that invokes a Tcl procedure that invokes \fBhistory\fR):  the
  1696. top-level command whose execution eventually resulted in a
  1697. \fBhistory\fR command is replaced.
  1698. If you wish to invoke commands like \fBhistory words\fR without
  1699. history revision, you can use \fBhistory event\fR to save the
  1700. current history event and then use \fBhistory change\fR to
  1701. restore it later.
  1702. .VE
  1703. .VE
  1704. .RE
  1705. .TP
  1706. \fBif \fItest \fR?\fBthen\fR? \fItrueBody \fR?\fBelse\fR? ?\fIfalseBody\fR?
  1707. The \fIif\fR command evaluates \fItest\fR as an expression (in the
  1708. same way that \fBexpr\fR evaluates its argument).  The value of the
  1709. expression must be numeric; if it
  1710. is non-zero then \fItrueBody\fR is called by passing it to the
  1711. Tcl interpreter.  Otherwise \fIfalseBody\fR is executed by passing it to
  1712. the Tcl interpreter.  The \fBthen\fR and \fBelse\fR arguments are optional
  1713. ``noise words'' to make the command easier to read.  \fIFalseBody\fR is
  1714. also optional; if it isn't specified then the command does nothing if
  1715. \fItest\fR evaluates to zero.  The return value from \fBif\fR is
  1716. the value of the last command executed in \fItrueBody\fR or
  1717. \fIfalseBody\fR, or the empty string if \fItest\fR evaluates to zero and
  1718. \fIfalseBody\fR isn't specified.
  1719. .TP
  1720. \fBincr \fIvarName \fR?\fIincrement\fR?
  1721. .VS
  1722. Increment the value stored in the variable whose name is \fIvarName\fR.
  1723. The value of the variable must be integral.
  1724. If \fIincrement\fR is supplied then its value (which must be an
  1725. integer) is added to the value of variable \fIvarName\fR;  otherwise
  1726. 1 is added to \fIvarName\fR.
  1727. The new value is stored as a decimal string in variable \fIvarName\fR
  1728. and also returned as result.
  1729. .VE
  1730. .TP
  1731. \fBinfo \fIoption \fR?\fIarg arg ...\fR?
  1732. Provide information about various internals to the Tcl interpreter.
  1733. The legal \fIoption\fR's (which may be abbreviated) are:
  1734. .RS
  1735. .TP
  1736. \fBinfo args \fIprocname\fR
  1737. Returns a list containing the names of the arguments to procedure
  1738. \fIprocname\fR, in order.  \fIProcname\fR must be the name of a
  1739. Tcl command procedure.
  1740. .TP
  1741. \fBinfo body \fIprocname\fR
  1742. Returns the body of procedure \fIprocname\fR.  \fIProcname\fR must be
  1743. the name of a Tcl command procedure.
  1744. .TP
  1745. \fBinfo cmdcount\fR
  1746. Returns a count of the total number of commands that have been invoked
  1747. in this interpreter.
  1748. .TP
  1749. \fBinfo commands \fR?\fIpattern\fR?
  1750. If \fIpattern\fR isn't specified, returns a list of names of all the
  1751. Tcl commands, including both the built-in commands written in C and
  1752. the command procedures defined using the \fBproc\fR command.
  1753. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1754. are returned.  Matching is determined using the same rules as for
  1755. \fBstring match\fR.
  1756. .TP
  1757. \fBinfo complete \fIcommand\fR
  1758. .VS
  1759. Returns 1 if \fIcommand\fR is a complete Tcl command in the sense of
  1760. having no unclosed quotes, braces, brackets or array element names,
  1761. If the command doesn't appear to be complete then 0 is returned.
  1762. This command is typically used in line-oriented input environments
  1763. to allow users to type in commands that span multiple lines;  if the
  1764. command isn't complete, the script can delay evaluating it until additional
  1765. lines have been typed to complete the command.
  1766. .VE
  1767. .TP
  1768. \fBinfo default \fIprocname arg varname\fR
  1769. \fIProcname\fR must be the name of a Tcl command procedure and \fIarg\fR
  1770. must be the name of an argument to that procedure.  If \fIarg\fR
  1771. doesn't have a default value then the command returns \fB0\fR.
  1772. Otherwise it returns \fB1\fR and places the default value of \fIarg\fR
  1773. into variable \fIvarname\fR.
  1774. .TP
  1775. \fBinfo exists \fIvarName\fR
  1776. Returns \fB1\fR if the variable named \fIvarName\fR exists in the
  1777. current context (either as a global or local variable), returns \fB0\fR
  1778. otherwise.
  1779. .TP
  1780. \fBinfo globals \fR?\fIpattern\fR?
  1781. If \fIpattern\fR isn't specified, returns a list of all the names
  1782. of currently-defined global variables.
  1783. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1784. are returned.  Matching is determined using the same rules as for
  1785. \fBstring match\fR.
  1786. .TP
  1787. \fBinfo level\fR ?\fInumber\fR?
  1788. If \fInumber\fR is not specified, this command returns a number
  1789. giving the stack level of the invoking procedure, or 0 if the
  1790. command is invoked at top-level.  If \fInumber\fR is specified,
  1791. then the result is a list consisting of the name and arguments for the
  1792. procedure call at level \fInumber\fR on the stack.  If \fInumber\fR
  1793. is positive then it selects a particular stack level (1 refers
  1794. to the top-most active procedure, 2 to the procedure it called, and
  1795. so on); otherwise it gives a level relative to the current level
  1796. (0 refers to the current procedure, -1 to its caller, and so on).
  1797. See the \fBuplevel\fR command for more information on what stack
  1798. levels mean.
  1799. .TP
  1800. \fBinfo library\fR
  1801. .VS
  1802. Returns the name of the library directory in which standard Tcl
  1803. scripts are stored.
  1804. The default value for the library is compiled into Tcl, but it
  1805. .VS
  1806. may be overridden by setting the TCL_LIBRARY environment variable.
  1807. If there is no TCL_LIBRARY variable and no compiled-in value then
  1808. and error is generated.
  1809. .VE
  1810. See the \fBlibrary\fR manual entry for details of the facilities
  1811. provided by the Tcl script library.
  1812. Normally each application will have its own application-specific
  1813. script library in addition to the Tcl script library;  I suggest that
  1814. each application set a global variable with a name like
  1815. .VS
  1816. \fB$\fIapp\fB_library\fR (where \fIapp\fR is the application's name)
  1817. .VE
  1818. to hold the location of that application's library directory.
  1819. .VE
  1820. .TP
  1821. \fBinfo locals \fR?\fIpattern\fR?
  1822. If \fIpattern\fR isn't specified, returns a list of all the names
  1823. of currently-defined local variables, including arguments to the
  1824. current procedure, if any.
  1825. .VS
  1826. Variables defined with the \fBglobal\fR and \fBupvar\fR commands
  1827. will not be returned.
  1828. .VE
  1829. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1830. are returned.  Matching is determined using the same rules as for
  1831. \fBstring match\fR.
  1832. .TP
  1833. \fBinfo procs \fR?\fIpattern\fR?
  1834. If \fIpattern\fR isn't specified, returns a list of all the
  1835. names of Tcl command procedures.
  1836. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1837. are returned.  Matching is determined using the same rules as for
  1838. \fBstring match\fR.
  1839. .TP
  1840. \fBinfo script\fR
  1841. .VS
  1842. If a Tcl script file is currently being evaluated (i.e. there is a
  1843. call to \fBTcl_EvalFile\fR active or there is an active invocation
  1844. of the \fBsource\fR command), then this command returns the name
  1845. of the innermost file being processed.  Otherwise the command returns an
  1846. empty string.
  1847. .VE
  1848. .TP
  1849. \fBinfo tclversion\fR
  1850. Returns the version number for this version of Tcl in the form \fIx.y\fR,
  1851. where changes to \fIx\fR represent major changes with probable
  1852. incompatibilities and changes to \fIy\fR represent small enhancements and
  1853. bug fixes that retain backward compatibility.
  1854. .TP
  1855. \fBinfo vars\fR ?\fIpattern\fR?
  1856. If \fIpattern\fR isn't specified,
  1857. returns a list of all the names of currently-visible variables, including
  1858. both locals and currently-visible globals.
  1859. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1860. are returned.  Matching is determined using the same rules as for
  1861. \fBstring match\fR.
  1862. .RE
  1863. .TP
  1864. \fBjoin \fIlist \fR?\fIjoinString\fR?
  1865. .VS
  1866. The \fIlist\fR argument must be a valid Tcl list.
  1867. This command returns the string
  1868. formed by joining all of the elements of \fIlist\fR together with
  1869. \fIjoinString\fR separating each adjacent pair of elements.
  1870. The \fIjoinString\fR argument defaults to a space character.
  1871. .VE
  1872. .TP
  1873. \fBlappend \fIvarName value \fR?\fIvalue value ...\fR?
  1874. .VS
  1875. Treat the variable given by \fIvarName\fR as a list and append
  1876. each of the \fIvalue\fR arguments to that list as a separate
  1877. element, with spaces between elements.
  1878. If \fIvarName\fR doesn't exist, it is created as a list with elements
  1879. given by the \fIvalue\fR arguments.
  1880. \fBLappend\fR is similar to \fBappend\fR except that the \fIvalue\fRs
  1881. are appended as list elements rather than raw text.
  1882. This command provides a relatively efficient way to build up
  1883. large lists.  For example, ``\fBlappend a $b\fR'' is much
  1884. more efficient than ``\fBset a [concat $a [list $b]]\fR'' when
  1885. \fB$a\fR is long.
  1886. .TP
  1887. \fBlindex \fIlist index\fR
  1888. Treats \fIlist\fR as a Tcl list and returns the \fIindex\fR'th element
  1889. from it (0 refers to the first element of the list).
  1890. In extracting the element, \fIlindex\fR observes the same rules
  1891. concerning braces and quotes and backslashes as the Tcl command
  1892. interpreter; however, variable
  1893. substitution and command substitution do not occur.
  1894. If \fIindex\fR is negative or greater than or equal to the number
  1895. of elements in \fIvalue\fR, then an empty
  1896. string is returned.
  1897. .TP
  1898. \fBlinsert \fIlist index element \fR?\fIelement element ...\fR?
  1899. This command produces a new list from \fIlist\fR by inserting all
  1900. of the \fIelement\fR arguments just before the \fIindex\fRth
  1901. element of \fIlist\fR.  Each \fIelement\fR argument will become
  1902. a separate element of the new list.  If \fIindex\fR is less than
  1903. or equal to zero, then the new elements are inserted at the
  1904. beginning of the list.  If \fIindex\fR is greater than or equal
  1905. to the number of elements in the list, then the new elements are
  1906. appended to the list.
  1907. .VE
  1908. .TP
  1909. \fBlist \fIarg \fR?\fIarg ...\fR?
  1910. This command returns a list comprised of all the \fIarg\fRs.  Braces
  1911. and backslashes get added as necessary, so that the \fBindex\fR command
  1912. may be used on the result to re-extract the original arguments, and also
  1913. so that \fBeval\fR may be used to execute the resulting list, with
  1914. \fIarg1\fR comprising the command's name and the other \fIarg\fRs comprising
  1915. its arguments.  \fBList\fR produces slightly different results than
  1916. \fBconcat\fR:  \fBconcat\fR removes one level of grouping before forming
  1917. the list, while \fBlist\fR works directly from the original arguments.
  1918. For example, the command
  1919. .RS
  1920. .DS
  1921. \fBlist a b {c d e} {f {g h}}
  1922. .DE
  1923. will return
  1924. .DS
  1925. \fBa b {c d e} {f {g h}}
  1926. .DE
  1927. while \fBconcat\fR with the same arguments will return
  1928. .DS
  1929. \fBa b c d e f {g h}\fR
  1930. .DE
  1931. .RE
  1932. .br
  1933. .VS
  1934. .TP
  1935. \fBllength \fIlist\fR
  1936. Treats \fIlist\fR as a list and returns a decimal string giving
  1937. the number of elements in it.
  1938. .TP
  1939. \fBlrange \fIlist first last
  1940. \fIList\fR must be a valid Tcl list.  This command will
  1941. return a new list consisting of elements
  1942. \fIfirst\fR through \fIlast\fR, inclusive.
  1943. \fILast\fR may be \fBend\fR (or any
  1944. abbreviation of it) to refer to the last element of the list.
  1945. If \fIfirst\fR is less than zero, it is treated as if it were zero.
  1946. If \fIlast\fR is greater than or equal to the number of elements
  1947. in the list, then it is treated as if it were \fBend\fR.
  1948. If \fIfirst\fR is greater than \fIlast\fR then an empty string
  1949. is returned.
  1950. Note: ``\fBlrange \fIlist first first\fR'' does not always produce the
  1951. same result as ``\fBlindex \fIlist first\fR'' (although it often does
  1952. for simple fields that aren't enclosed in braces); it does, however,
  1953. produce exactly the same results as ``\fBlist [lindex \fIlist first\fB]\fR''
  1954. .TP
  1955. \fBlreplace \fIlist first last \fR?\fIelement element ...\fR?
  1956. Returns a new list formed by replacing one or more elements of
  1957. \fIlist\fR with the \fIelement\fR arguments.
  1958. \fIFirst\fR gives the index in \fIlist\fR of the first element
  1959. to be replaced.
  1960. If \fIfirst\fR is less than zero then it refers to the first
  1961. element of \fIlist\fR;  the element indicated by \fIfirst\fR
  1962. must exist in the list.
  1963. \fILast\fR gives the index in \fIlist\fR of the last element
  1964. to be replaced;  it must be greater than or equal to \fIfirst\fR.
  1965. \fILast\fR may be \fBend\fR (or any abbreviation of it) to indicate
  1966. that all elements between \fIfirst\fR and the end of the list should
  1967. be replaced.
  1968. The \fIelement\fR arguments specify zero or more new arguments to
  1969. be added to the list in place of those that were deleted.
  1970. Each \fIelement\fR argument will become a separate element of
  1971. the list.
  1972. If no \fIelement\fR arguments are specified, then the elements
  1973. between \fIfirst\fR and \fIlast\fR are simply deleted.
  1974. .TP
  1975. \fBlsearch \fIlist pattern\fR
  1976. Search the elements of \fIlist\fR to see if one of them matches
  1977. \fIpattern\fR.
  1978. If so, the command returns the index of the first matching
  1979. element.
  1980. If not, the command returns \fB\-1\fR.
  1981. Pattern matching is done in the same way as for the \fBstring match\fR
  1982. command.
  1983. .TP
  1984. \fBlsort \fIlist\fR
  1985. Sort the elements of \fIlist\fR, returning a new list in sorted
  1986. order.
  1987. ASCII sorting is used, with the result in increasing order.
  1988. .VE
  1989. .TP
  1990. \fBopen \fIfileName\fR ?\fIaccess\fR?
  1991. .VS
  1992. Opens a file and returns an identifier
  1993. that may be used in future invocations
  1994. of commands like \fBread\fR, \fBputs\fR, and \fBclose\fR.
  1995. \fIFileName\fR gives the name of the file to open; if it starts with
  1996. a tilde then tilde substitution is performed as described for
  1997. \fBTcl_TildeSubst\fR.
  1998. If the first character of \fIfileName\fR is ``|'' then the
  1999. remaining characters of \fIfileName\fR are treated as a command
  2000. pipeline to invoke, in the same style as for \fBexec\fR.
  2001. In this case, the identifier returned by \fBopen\fR may be used
  2002. to write to the command's input pipe or read from its output pipe.
  2003. The \fIaccess\fR argument indicates the way in which the file
  2004. (or command pipeline) is to be accessed.
  2005. It may have any of the following values:
  2006. .RS
  2007. .TP
  2008. \fBr\fR
  2009. Open the file for reading only; the file must already exist.
  2010. .TP
  2011. \fBr+\fR
  2012. Open the file for both reading and writing; the file must
  2013. already exist.
  2014. .TP
  2015. \fBw\fR
  2016. Open the file for writing only.  Truncate it if it exists.  If it doesn't
  2017. exist, create a new file.
  2018. .TP
  2019. \fBw+\fR
  2020. Open the file for reading and writing.  Truncate it if it exists.
  2021. If it doesn't exist, create a new file.
  2022. .TP
  2023. \fBa\fR
  2024. Open the file for writing only.  The file must already exist, and the file
  2025. is positioned so that new data is appended to the file.
  2026. .TP
  2027. \fBa+\fR
  2028. Open the file for reading and writing.  If the file doesn't exist,
  2029. create a new empty file.
  2030. Set the initial access position  to the end of the file.
  2031. .PP
  2032. \fIAccess\fR defaults to \fBr\fR.
  2033. If a file is opened for both reading and writing, then \fBseek\fR
  2034. must be invoked between a read and a write, or vice versa (this
  2035. restriction does not apply to command pipelines opened with \fBopen\fR).
  2036. When \fIfileName\fR specifies a command pipeline and a write-only access
  2037. is used, then standard output from the pipeline is directed to the
  2038. current standard output unless overridden by the command.
  2039. When \fIfileName\fR specifies a command pipeline and a read-only access
  2040. is used, then standard input from the pipeline is taken from the
  2041. current standard input unless overridden by the command.
  2042. .RE
  2043. .VE
  2044. .TP
  2045. \fBproc \fIname args body\fR
  2046. The \fBproc\fR command creates a new Tcl command procedure,
  2047. \fIname\fR, replacing
  2048. any existing command there may have been by that name.  Whenever the
  2049. new command is invoked, the contents of \fIbody\fR will be executed
  2050. by the Tcl interpreter.  \fIArgs\fR specifies the formal arguments to the
  2051. procedure.  It consists of a list, possibly empty, each of whose
  2052. elements specifies
  2053. one argument.  Each argument specifier is also a list with either
  2054. one or two fields.  If there is only a single field in the specifier,
  2055. then it is the name of the argument; if there are two fields, then
  2056. the first is the argument name and the second is its default value.
  2057. braces and backslashes may be used in the usual way to specify
  2058. complex default values.
  2059. .IP
  2060. When \fIname\fR is invoked, a local variable
  2061. will be created for each of the formal arguments to the procedure; its
  2062. value will be the value of corresponding argument in the invoking command
  2063. or the argument's default value.
  2064. Arguments with default values need not be
  2065. specified in a procedure invocation.  However, there must be enough
  2066. actual arguments for all the
  2067. formal arguments that don't have defaults, and there must not be any extra
  2068. actual arguments.  There is one special case to permit procedures with
  2069. variable numbers of arguments.  If the last formal argument has the name
  2070. \fBargs\fR, then a call to the procedure may contain more actual arguments
  2071. than the procedure has formals.  In this case, all of the actual arguments
  2072. starting at the one that would be assigned to \fBargs\fR are combined into
  2073. a list (as if the \fBlist\fR command had been used); this combined value
  2074. is assigned to the local variable \fBargs\fR.
  2075. .IP
  2076. When \fIbody\fR is being executed, variable names normally refer to
  2077. local variables, which are created automatically when referenced and
  2078. deleted when the procedure returns.  One local variable is automatically
  2079. created for each of the procedure's arguments.
  2080. Global variables can only be accessed by invoking
  2081. the \fBglobal\fR command.
  2082. .IP
  2083. The \fBproc\fR command returns the null string.  When a procedure is
  2084. invoked, the procedure's return value is the value specified in a
  2085. \fBreturn\fR command.  If the procedure doesn't execute an explicit
  2086. \fBreturn\fR, then its return value is the value of the last command
  2087. executed in the procedure's body.
  2088. If an error occurs while executing the procedure
  2089. body, then the procedure-as-a-whole will return that same error.
  2090. .TP
  2091. \fBputs \fIfileId string \fR?\fBnonewline\fR?
  2092. .VS
  2093. Writes the characters given by \fIstring\fR to the file given
  2094. by \fIfileId\fR.
  2095. \fBPuts\fR normally outputs a newline character after \fIstring\fR,
  2096. but this feature may be suppressed by specifying the \fBnonewline\fR
  2097. argument.
  2098. Output to files is buffered internally by Tcl; the \fBflush\fR
  2099. command may be used to force buffered characters to be output.
  2100. \fIFileId\fR must have been the return
  2101. value from a previous call to \fBopen\fR, or it may be
  2102. \fBstdout\fR or \fBstderr\fR to refer to one of the standard I/O
  2103. channels; it must refer to a file that was opened for
  2104. writing.
  2105. .TP
  2106. \fBpwd\fR
  2107. .br
  2108. Returns the path name of the current working directory.
  2109. .TP
  2110. \fBread \fIfileId\fR
  2111. .TP
  2112. \fBread \fIfileId \fBnonewline\fR
  2113. .TP
  2114. \fBread \fIfileId numBytes\fR
  2115. In the first form, all of the remaining bytes are read from the file
  2116. given by \fIfileId\fR; they are returned as the result of the command.
  2117. If \fBnonewline\fR is specified as an additional argument, then the last
  2118. character of the file is discarded if it is a newline.
  2119. In the third form, the extra argument specifies how many bytes to read;
  2120. exactly this many bytes will be read and returned, unless there are fewer than
  2121. \fInumBytes\fR bytes left in the file; in this case, all the remaining
  2122. bytes are returned.
  2123. \fIFileId\fR must be \fBstdin\fR or the return
  2124. value from a previous call to \fBopen\fR; it must
  2125. refer to a file that was opened for reading.
  2126. .TP
  2127. \fBregexp \fR?\fB\-indices\fR? \fR?\fB\-nocase\fR? \fIexp string \fR?\fImatchVar\fR? ?\fIsubMatchVar subMatchVar ...\fR?
  2128. Determines whether the regular expression \fIexp\fR matches part or
  2129. all of \fIstring\fR and returns 1 if it does, 0 if it doesn't.
  2130. See REGULAR EXPRESSIONS above for complete information on the
  2131. syntax of \fIexp\fR and how it is matched against \fIstring\fR.
  2132. .RS
  2133. .LP
  2134. If the \fB\-nocase\fR switch is specified then upper-case
  2135. characters in \fIstring\fR
  2136. are treated as lower case during the matching process.
  2137. The \fB\-nocase\fR switch must be specified before \fIexp\fR and
  2138. may not be abbreviated.
  2139. .LP
  2140. If additional arguments are specified after \fIstring\fR then they
  2141. are treated as the names of variables to use to return
  2142. information about which part(s) of \fIstring\fR matched \fIexp\fR.
  2143. \fIMatchVar\fR will be set to the range of \fIstring\fR that
  2144. matched all of \fIexp\fR.  The first \fIsubMatchVar\fR will contain
  2145. the characters in \fIstring\fR that matched the leftmost parenthesized
  2146. subexpression within \fIexp\fR, the next \fIsubMatchVar\fR will
  2147. contain the characters that matched the next parenthesized
  2148. subexpression to the right in \fIexp\fR, and so on.
  2149. .LP
  2150. Normally, \fImatchVar\fR and the \fIsubMatchVar\fRs are set to hold
  2151. the matching characters from \fBstring\fR.
  2152. However, if the \fB\-indices\fR switch is specified then each variable
  2153. will contain a list of two decimal strings giving the indices
  2154. in \fIstring\fR of the first and last characters in the matching
  2155. range of characters.
  2156. The \fB\-indices\fR switch must be specified before the \fIexp\fR
  2157. argument and may not be abbreviated.
  2158. .LP
  2159. If there are more \fIsubMatchVar\fR's than parenthesized
  2160. subexpressions within \fIexp\fR, or if a particular subexpression
  2161. in \fIexp\fR doesn't match the string (e.g. because it was in a
  2162. portion of the expression that wasn't matched), then the corresponding
  2163. \fIsubMatchVar\fR will be set to ``\fB\-1 \-1\fR'' if \fB\-indices\fR
  2164. has been specified or to an empty string otherwise.
  2165. .RE
  2166. .TP
  2167. \fBregsub \fR?\fB\-all\fR? ?\fB\-nocase\fR? \fIexp string subSpec varName\fR
  2168. This command matches the regular expression \fIexp\fR against
  2169. \fIstring\fR using the rules described in REGULAR EXPRESSIONS
  2170. above.
  2171. If there is no match, then the command returns 0 and does nothing
  2172. else.
  2173. If there is a match, then the command returns 1 and also copies
  2174. \fIstring\fR to the variable whose name is given by \fIvarName\fR.
  2175. When copying \fIstring\fR, the portion of \fIstring\fR that
  2176. matched \fIexp\fR is replaced with \fIsubSpec\fR.
  2177. If \fIsubSpec\fR contains a ``&'' or ``\e0'', then it is replaced
  2178. in the substitution with the portion of \fIstring\fR that
  2179. matched \fIexp\fR.
  2180. If \fIsubSpec\fR contains a ``\e\fIn\fR'', where \fIn\fR is a digit
  2181. between 1 and 9, then it is replaced in the substitution with
  2182. the portion of \fIstring\fR that matched the \fIn\fR-th
  2183. parenthesized subexpression of \fIexp\fR.
  2184. Additional backslashes may be used in \fIsubSpec\fR to prevent special
  2185. interpretation of ``&'' or ``\e0'' or ``\e\fIn\fR'' or
  2186. backslash.
  2187. The use of backslashes in \fIsubSpec\fR tends to interact badly
  2188. with the Tcl parser's use of backslashes, so it's generally
  2189. safest to enclose \fIsubSpec\fR in braces if it includes
  2190. backslashes.
  2191. If the \fB\-all\fR argument is specified, then all ranges in
  2192. \fIstring\fR that match \fIexp\fR are found and substitution is
  2193. performed for each of these ranges;  otherwise only the first
  2194. matching range is found and substituted.
  2195. If \fB\-all\fR is specified, then ``&'' and ``\e\fIn\fR''
  2196. sequences are handled for each substitution using the information
  2197. from the corresponding match.
  2198. If the \fB\-nocase\fR argument is specified, then upper-case
  2199. characters in \fIstring\fR are converted to lower-case before
  2200. matching against \fIexp\fR;  however, substitutions specified
  2201. by \fIsubSpec\fR use the original unconverted form of \fIstring\fR.
  2202. The \fB\-all\fR and \fB\-nocase\fR arguments must be specified
  2203. exactly:  no abbreviations are permitted.
  2204. .VE
  2205. .TP
  2206. \fBrename \fIoldName newName\fR
  2207. Rename the command that used to be called \fIoldName\fR so that it
  2208. is now called \fInewName\fR.  If \fInewName\fR is an empty string
  2209. (e.g. {}) then \fIoldName\fR is deleted.  The \fBrename\fR command
  2210. returns an empty string as result.
  2211. .TP
  2212. \fBreturn \fR?\fIvalue\fR?
  2213. Return immediately from the current procedure
  2214. (or top-level command or \fBsource\fR command),
  2215. with \fIvalue\fR as the return value.  If \fIvalue\fR is not specified,
  2216. an empty string will be returned as result.
  2217. .TP
  2218. \fBscan \fIstring format varname1 \fR?\fIvarname2 ...\fR?
  2219. This command parses fields from an input string in the same fashion
  2220. as the C \fBsscanf\fR procedure.  \fIString\fR gives the input to
  2221. be parsed and \fIformat\fR indicates how to parse it, using \fB%\fR
  2222. fields as in \fBsscanf\fR.  All of the \fBsscanf\fR options are valid;
  2223. see the \fBsscanf\fR man page for details.  Each \fIvarname\fR gives
  2224. the name of a variable; when a field is scanned from \fIstring\fR,
  2225. the result is converted back into a string and assigned to the
  2226. corresponding \fIvarname\fR.  The only unusual conversion is for
  2227. \fB%c\fR.  For \fB%c\fR conversions a single character value is
  2228. converted to a decimal string, which is then assigned to the
  2229. corresponding \fIvarname\fR;
  2230. .VS
  2231. no field width may be specified for this conversion.
  2232. .TP
  2233. \fBseek \fIfileId offset \fR?\fIorigin\fR?
  2234. Change the current access position for \fIfileId\fR.
  2235. The \fIoffset\fR and \fIorigin\fR arguments specify the position at
  2236. which the next read or write will occur for \fIfileId\fR.
  2237. \fIOffset\fR must be a number (which may be negative) and \fIorigin\fR
  2238. must be one of the following:
  2239. .RS
  2240. .TP
  2241. \fBstart\fR
  2242. The new access position will be \fIoffset\fR bytes from the start
  2243. of the file.
  2244. .TP
  2245. \fBcurrent\fR
  2246. The new access position will be \fIoffset\fR bytes from the current
  2247. access position; a negative \fIoffset\fR moves the access position
  2248. backwards in the file.
  2249. .TP
  2250. \fBend\fR
  2251. The new access position will be \fIoffset\fR bytes from the end of
  2252. the file.  A negative \fIoffset\fR places the access position before
  2253. the end-of-file, and a positive \fIoffset\fR places the access position
  2254. after the end-of-file.
  2255. .LP
  2256. The \fIorigin\fR argument defaults to \fBstart\fR.
  2257. \fIFileId\fR must have been the return
  2258. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  2259. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  2260. channels.
  2261. This command returns an empty string.
  2262. .RE
  2263. .VE
  2264. .TP
  2265. \fBset \fIvarname \fR?\fIvalue\fR?
  2266. Returns the value of variable \fIvarname\fR.
  2267. If \fIvalue\fR is specified, then set
  2268. the value of \fIvarname\fR to \fIvalue\fR, creating a new variable
  2269. if one doesn't already exist, and return its value.
  2270. .VS
  2271. If \fIvarName\fR contains an open parenthesis and ends with a
  2272. close parenthesis, then it refers to an array element:  the characters
  2273. before the open parenthesis are the name of the array, and the characters
  2274. between the parentheses are the index within the array.
  2275. Otherwise \fIvarName\fR refers to a scalar variable.
  2276. .VE
  2277. If no procedure is active, then \fIvarname\fR refers to a global
  2278. variable.
  2279. If a procedure is active, then \fIvarname\fR refers to a parameter
  2280. or local variable of the procedure, unless the \fIglobal\fR command
  2281. has been invoked to declare \fIvarname\fR to be global.
  2282. .TP
  2283. \fBsource \fIfileName\fR
  2284. Read file \fIfileName\fR and pass the contents to the Tcl interpreter
  2285. as a sequence of commands to execute in the normal fashion.  The return
  2286. value of \fBsource\fR is the return value of the last command executed
  2287. from the file.  If an error occurs in executing the contents of the
  2288. file, then the \fBsource\fR command will return that error.
  2289. If a \fBreturn\fR command is invoked from within the file, the remainder of
  2290. the file will be skipped and the \fBsource\fR command will return
  2291. normally with the result from the \fBreturn\fR command.
  2292. If \fIfileName\fR starts with a tilde, then it is tilde-substituted
  2293. as described in the \fBTcl_TildeSubst\fR manual entry.
  2294. .TP
  2295. \fBsplit \fIstring \fR?\fIsplitChars\fR?
  2296. Returns a list created by splitting \fIstring\fR at each character
  2297. that is in the \fIsplitChars\fR argument.
  2298. Each element of the result list will consist of the
  2299. characters from \fIstring\fR between instances of the
  2300. characters in \fIsplitChars\fR.
  2301. Empty list elements will be generated if \fIstring\fR contains
  2302. adjacent characters in \fIsplitChars\fR, or if the first or last
  2303. character of \fIstring\fR is in \fIsplitChars\fR.
  2304. If \fIsplitChars\fR is an empty string then each character of
  2305. \fIstring\fR becomes a separate element of the result list.
  2306. \fISplitChars\fR defaults to the standard white-space characters.
  2307. For example,
  2308. .RS
  2309. .DS
  2310. \fBsplit "comp.unix.misc" .\fR
  2311. .DE
  2312. returns \fB"comp unix misc"\fR and
  2313. .DS
  2314. \fBsplit "Hello world" {}\fR
  2315. .DE
  2316. returns \fB"H e l l o { } w o r l d"\fR.
  2317. .VE
  2318. .RE
  2319. .TP
  2320. \fBstring \fIoption arg \fR?\fIarg ...?\fR
  2321. Perform one of several string operations, depending on \fIoption\fR.
  2322. The legal \fIoption\fRs (which may be abbreviated) are:
  2323. .RS
  2324. .TP
  2325. \fBstring compare \fIstring1 string2\fR
  2326. Perform a character-by-character comparison of strings \fIstring1\fR and
  2327. \fIstring2\fR in the same way as the C \fBstrcmp\fR procedure.  Return
  2328. -1, 0, or 1, depending on whether \fIstring1\fR is lexicographically
  2329. less than, equal to, or greater than \fIstring2\fR.
  2330. .TP
  2331. \fBstring first \fIstring1 string2\fR
  2332. Search \fIstring2\fR for a sequence of characters that exactly match
  2333. the characters in \fIstring1\fR.  If found, return the index of the
  2334. first character in the first such match within \fIstring2\fR.  If not
  2335. found, return -1.
  2336. .br
  2337. .VS
  2338. .TP
  2339. \fBstring index \fIstring charIndex\fR
  2340. Returns the \fIcharIndex\fR'th character of the \fIstring\fR
  2341. argument.  A \fIcharIndex\fR of 0 corresponds to the first
  2342. character of the string.
  2343. If \fIcharIndex\fR is less than 0 or greater than
  2344. or equal to the length of the string then an empty string is
  2345. returned.
  2346. .VE
  2347. .TP
  2348. \fBstring last \fIstring1 string2\fR
  2349. Search \fIstring2\fR for a sequence of characters that exactly match
  2350. the characters in \fIstring1\fR.  If found, return the index of the
  2351. first character in the last such match within \fIstring2\fR.  If there
  2352. is no match, then return \-1.
  2353. .br
  2354. .VS
  2355. .TP
  2356. \fBstring length \fIstring\fR
  2357. Returns a decimal string giving the number of characters in \fIstring\fR.
  2358. .VE
  2359. .TP
  2360. \fBstring match \fIpattern\fR \fIstring\fR
  2361. See if \fIpattern\fR matches \fIstring\fR; return 1 if it does, 0
  2362. if it doesn't.  Matching is done in a fashion similar to that
  2363. used by the C-shell.  For the two strings to match, their contents
  2364. must be identical except that the following special sequences
  2365. may appear in \fIpattern\fR:
  2366. .RS
  2367. .IP \fB*\fR 10
  2368. Matches any sequence of characters in \fIstring\fR,
  2369. including a null string.
  2370. .IP \fB?\fR 10
  2371. Matches any single character in \fIstring\fR.
  2372. .IP \fB[\fIchars\fB]\fR 10
  2373. Matches any character in the set given by \fIchars\fR.  If a sequence
  2374. of the form
  2375. \fIx\fB\-\fIy\fR appears in \fIchars\fR, then any character
  2376. between \fIx\fR and \fIy\fR, inclusive, will match.
  2377. .IP \fB\e\fIx\fR 10
  2378. Matches the single character \fIx\fR.  This provides a way of
  2379. avoiding the special interpretation of the characters
  2380. \fB*?[]\e\fR in \fIpattern\fR.
  2381. .RE
  2382. .br
  2383. .VS
  2384. .TP
  2385. \fBstring range \fIstring first last\fR
  2386. Returns a range of consecutive characters from \fIstring\fR, starting
  2387. with the character whose index is \fIfirst\fR and ending with the
  2388. character whose index is \fIlast\fR.  An index of 0 refers to the
  2389. first character of the string.  \fILast\fR may be \fBend\fR (or any
  2390. abbreviation of it) to refer to the last character of the string.
  2391. If \fIfirst\fR is less than zero then it is treated as if it were zero, and
  2392. if \fIlast\fR is greater than or equal to the length of the string then
  2393. it is treated as if it were \fBend\fR.  If \fIfirst\fR is greater than
  2394. \fIlast\fR then an empty string is returned.
  2395. .TP
  2396. \fBstring tolower \fIstring\fR
  2397. Returns a value equal to \fIstring\fR except that all upper case
  2398. letters have been converted to lower case.
  2399. .TP
  2400. \fBstring toupper \fIstring\fR
  2401. Returns a value equal to \fIstring\fR except that all lower case
  2402. letters have been converted to upper case.
  2403. .TP
  2404. \fBstring trim \fIstring\fR ?\fIchars\fR?
  2405. Returns a value equal to \fIstring\fR except that any leading
  2406. or trailing characters from the set given by \fIchars\fR are
  2407. removed.
  2408. If \fIchars\fR is not specified then white space is removed
  2409. (spaces, tabs, newlines, and carriage returns).
  2410. .TP
  2411. \fBstring trimleft \fIstring\fR ?\fIchars\fR?
  2412. Returns a value equal to \fIstring\fR except that any
  2413. leading characters from the set given by \fIchars\fR are
  2414. removed.
  2415. If \fIchars\fR is not specified then white space is removed
  2416. (spaces, tabs, newlines, and carriage returns).
  2417. .TP
  2418. \fBstring trimright \fIstring\fR ?\fIchars\fR?
  2419. Returns a value equal to \fIstring\fR except that any
  2420. trailing characters from the set given by \fIchars\fR are
  2421. removed.
  2422. If \fIchars\fR is not specified then white space is removed
  2423. (spaces, tabs, newlines, and carriage returns).
  2424. .RE
  2425. .TP
  2426. \fBtell \fIfileId\fR
  2427. Returns a decimal string giving the current access position in
  2428. \fIfileId\fR.
  2429. \fIFileId\fR must have been the return
  2430. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  2431. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  2432. channels.
  2433. .VE
  2434. .TP
  2435. \fBtime \fIcommand\fR ?\fIcount\fR?
  2436. This command will call the Tcl interpreter \fIcount\fR
  2437. times to execute \fIcommand\fR (or once if \fIcount\fR isn't
  2438. specified).  It will then return a string of the form
  2439. .RS
  2440. .DS
  2441. \fB503 microseconds per iteration\fR
  2442. .DE
  2443. which indicates the average amount of time required per iteration,
  2444. in microseconds.
  2445. Time is measured in elapsed time, not CPU time.
  2446. .RE
  2447. .TP
  2448. \fBtrace \fIoption\fR ?\fIarg arg ...\fR?
  2449. .VS
  2450. Cause Tcl commands to be executed whenever certain operations are
  2451. invoked.  At present, only variable tracing is implemented. The
  2452. legal \fIoption\fR's (which may be abbreviated) are:
  2453. .RS
  2454. .TP
  2455. \fBtrace variable \fIname ops command\fR
  2456. Arrange for \fIcommand\fR to be executed whenever variable \fIname\fR
  2457. is accessed in one of the ways given by \fIops\fR.  \fIName\fR may
  2458. refer to a normal variable, an element of an array, or to an array
  2459. as a whole (i.e. \fIname\fR may be just the name of an array, with no
  2460. parenthesized index).  If \fIname\fR refers to a whole array, then
  2461. \fIcommand\fR is invoked whenever any element of the array is
  2462. manipulated.
  2463. .RS
  2464. .LP
  2465. \fIOps\fR indicates which operations are of interest, and consists of
  2466. one or more of the following letters:
  2467. .RS
  2468. .TP
  2469. \fBr\fR
  2470. Invoke \fIcommand\fR whenever the variable is read.
  2471. .TP
  2472. \fBw\fR
  2473. Invoke \fIcommand\fR whenever the variable is written.
  2474. .TP
  2475. \fBu\fR
  2476. Invoke \fIcommand\fR whenever the variable is unset.  Variables
  2477. can be unset explicitly with the \fBunset\fR command, or
  2478. implicitly when procedures return (all of their local variables
  2479. are unset).  Variables are also unset when interpreters are
  2480. deleted, but traces will not be invoked because there is no
  2481. interpreter in which to execute them.
  2482. .RE
  2483. .LP
  2484. When the trace triggers, three arguments are appended to
  2485. \fIcommand\fR so that the actual command is as follows:
  2486. .DS C
  2487. \fIcommand name1 name2 op\fR
  2488. .DE
  2489. \fIName1\fR and \fIname2\fR give the name(s) for the variable
  2490. being accessed:  if the variable is a scalar then \fIname1\fR
  2491. gives the variable's name and \fIname2\fR is an empty string;
  2492. if the variable is an array element then \fIname1\fR gives the
  2493. name of the array and name2 gives the index into the array;
  2494. if an entire array is being deleted and the trace was registered
  2495. on the overall array, rather than a single element, then \fIname1\fR
  2496. gives the array name and \fIname2\fR is an empty string.
  2497. \fIOp\fR indicates what operation is being performed on the
  2498. variable, and is one of \fBr\fR, \fBw\fR, or \fBu\fR as
  2499. defined above.
  2500. .LP
  2501. \fICommand\fR executes in the same context as the code that invoked
  2502. the traced operation:  if the variable was accessed as part of a
  2503. Tcl procedure, then \fIcommand\fR will have access to the same
  2504. local variables as code in the procedure.  This context may be
  2505. different than the context in which the trace was created.
  2506. If \fIcommand\fR invokes a procedure (which it normally does) then
  2507. the procedure will have to use \fBupvar\fR or \fBuplevel\fR if it
  2508. wishes to access the traced variable.
  2509. Note also that \fIname1\fR may not necessarily be the same as the name
  2510. used to set the trace on the variable;  differences can occur if
  2511. the access is made through a variable defined with the \fBupvar\fR
  2512. command.
  2513. .LP
  2514. For read and write traces, \fIcommand\fR can modify
  2515. the variable to affect the result of the traced operation.
  2516. If \fIcommand\fR modifies the value of a variable during a
  2517. read or write trace, then the new value will be returned as the
  2518. result of the traced operation.
  2519. The return value from  \fIcommand\fR is ignored except that
  2520. if it returns an error of any sort then the traced operation
  2521. is aborted with an error message saying that the access was denied
  2522. (this mechanism can be used to implement read-only variables, for
  2523. example).
  2524. For write traces, \fIcommand\fR is invoked after the variable's
  2525. value has been changed; it can write a new value into the variable
  2526. to override the original value specified in the write operation.
  2527. To implement read-only variables, \fIcommand\fR will have to restore
  2528. the old value of the variable.
  2529. .LP
  2530. While \fIcommand\fR is executing during a read or write trace, traces
  2531. on the variable are temporarily disabled.
  2532. This means that reads and writes invoked by
  2533. \fIcommand\fR will occur directly, without invoking \fIcommand\fR
  2534. (or any other traces) again.
  2535. .LP
  2536. When an unset trace is invoked, the variable has already been
  2537. deleted:  it will appear to be undefined with no traces.
  2538. If an unset occurs because of a procedure return, then the
  2539. trace will be invoked in the variable context of the procedure
  2540. being returned to:  the stack frame of the returning procedure
  2541. will no longer exist.
  2542. Traces are not disabled during unset traces, so if an unset trace
  2543. command creates a new trace and accesses the variable, the
  2544. trace will be invoked.
  2545. .LP
  2546. If there are multiple traces on a variable they are invoked
  2547. in order of creation, most-recent first.
  2548. If one trace returns an error, then no further traces are
  2549. invoked for the variable.
  2550. If an array element has a trace set, and there is also a trace
  2551. set on the array as a whole, the trace on the overall array
  2552. is invoked before the one on the element.
  2553. .LP
  2554. Once created, the trace remains in effect either until the
  2555. trace is removed with the \fBtrace vdelete\fR command described
  2556. below, until the variable is unset, or until the interpreter
  2557. is deleted.
  2558. Unsetting an element of array will remove any traces on that
  2559. element, but will not remove traces on the overall array.
  2560. .LP
  2561. This command returns an empty string.
  2562. .RE
  2563. .TP
  2564. \fBtrace vdelete \fIname ops command\fR
  2565. If there is a trace set on variable \fIname\fR with the
  2566. operations and command given by \fIops\fR and \fIcommand\fR,
  2567. then the trace is removed, so that \fIcommand\fR will never
  2568. again be invoked.
  2569. Returns an empty string.
  2570. .TP
  2571. \fBtrace vinfo \fIname\fR
  2572. Returns a list containing one element for each trace
  2573. currently set on variable \fIname\fR.
  2574. Each element of the list is itself a list containing two
  2575. elements, which are the \fIops\fR and \fIcommand\fR associated
  2576. with the trace.
  2577. If \fIname\fR doesn't exist or doesn't have any traces set, then
  2578. the result of the command will be an empty string.
  2579. .RE
  2580. .TP
  2581. \fBunknown \fIcmdName \fR?\fIarg arg ...\fR?
  2582. This command doesn't actually exist as part of Tcl, but Tcl will
  2583. invoke it if it does exist.
  2584. If the Tcl interpreter encounters a command name for which there
  2585. is not a defined command, then Tcl checks for the existence of
  2586. a command named \fBunknown\fR.
  2587. If there is no such command, then the interpeter returns an
  2588. error.
  2589. If the \fBunknown\fR command exists, then it is invoked with
  2590. arguments consisting of the fully-substituted name and arguments
  2591. for the original non-existent command.
  2592. The \fBunknown\fR command typically does things like searching
  2593. through library directories for a command procedure with the name
  2594. \fIcmdName\fR, or expanding abbreviated command names to full-length,
  2595. or automatically executing unknown commands as UNIX sub-processes.
  2596. In some cases (such as expanding abbreviations) \fBunknown\fR will
  2597. change the original command slightly and then (re-)execute it.
  2598. The result of the \fBunknown\fR command is used as the result for
  2599. the original non-existent command.
  2600. .TP
  2601. \fBunset \fIname \fR?\fIname name ...\fR?
  2602. Remove one or more variables.
  2603. Each \fIname\fR is a variable name, specified in any of the
  2604. ways acceptable to the \fBset\fR command.
  2605. If a \fIname\fR refers to an element of an array, then that
  2606. element is removed without affecting the rest of the array.
  2607. If a \fIname\fR consists of an array name with no parenthesized
  2608. index, then the entire array is deleted.
  2609. The \fBunset\fR command returns an empty string as result.
  2610. An error occurs if any of the variables doesn't exist.
  2611. .VE
  2612. .TP
  2613. \fBuplevel \fR?\fIlevel\fR?\fI command \fR?\fIcommand ...\fR?
  2614. All of the \fIcommand\fR arguments are concatenated as if they had
  2615. been passed to \fBconcat\fR; the result is then evaluated in the
  2616. variable context indicated by \fIlevel\fR.  \fBUplevel\fR returns
  2617. the result of that evaluation.  If \fIlevel\fR is an integer, then
  2618. it gives a distance (up the procedure calling stack) to move before
  2619. executing the command.  If \fIlevel\fR consists of \fB#\fR followed by
  2620. a number then the number gives an absolute level number.  If \fIlevel\fR
  2621. is omitted then it defaults to \fB1\fR.  \fILevel\fR cannot be
  2622. defaulted if the first \fIcommand\fR argument starts with a digit or \fB#\fR.
  2623. For example, suppose that procedure \fBa\fR was invoked
  2624. from top-level, and that it called \fBb\fR, and that \fBb\fR called \fBc\fR.
  2625. Suppose that \fBc\fR invokes the \fBuplevel\fR command.  If \fIlevel\fR
  2626. is \fB1\fR or \fB#2\fR  or omitted, then the command will be executed
  2627. in the variable context of \fBb\fR.  If \fIlevel\fR is \fB2\fR or \fB#1\fR
  2628. then the command will be executed in the variable context of \fBa\fR.
  2629. If \fIlevel\fR is \fB3\fR or \fB#0\fR then the command will be executed
  2630. at top-level (only global variables will be visible).
  2631. The \fBuplevel\fR command causes the invoking procedure to disappear
  2632. from the procedure calling stack while the command is being executed.
  2633. In the above example, suppose \fBc\fR invokes the command
  2634. .RS
  2635. .DS
  2636. \fBuplevel 1 {set x 43; d}
  2637. .DE
  2638. where \fBd\fR is another Tcl procedure.  The \fBset\fR command will
  2639. modify the variable \fBx\fR in \fBb\fR's context, and \fBd\fR will execute
  2640. at level 3, as if called from \fBb\fR.  If it in turn executes
  2641. the command
  2642. .DS
  2643. \fBuplevel {set x 42}
  2644. .DE
  2645. then the \fBset\fR command will modify the same variable \fBx\fR in \fBb\fR's
  2646. context:  the procedure \fBc\fR does not appear to be on the call stack
  2647. when \fBd\fR is executing.  The command ``\fBinfo level\fR'' may
  2648. be used to obtain the level of the current procedure.
  2649. \fBUplevel\fR makes it possible to implement new control
  2650. constructs as Tcl procedures (for example, \fBuplevel\fR could
  2651. be used to implement the \fBwhile\fR construct as a Tcl procedure).
  2652. .RE
  2653. .TP
  2654. \fBupvar \fR?\fIlevel\fR? \fIotherVar myVar \fR?\fIotherVar myVar \fR...?
  2655. .VS
  2656. This command arranges for one or more local variables in the current
  2657. procedure to refer to variables in an enclosing procedure call or
  2658. to global variables.
  2659. \fILevel\fR may have any of the forms permitted for the \fBuplevel\fR
  2660. command, and may be omitted if the first letter of the first \fIotherVar\fR
  2661. isn't \fB#\fR or a digit (it defaults to \fB1\fR).
  2662. For each \fIotherVar\fR argument, \fBupvar\fR makes the variable
  2663. by that name in the procedure frame given by \fIlevel\fR (or at
  2664. global level, if \fIlevel\fR is \fB#0\fR) accessible
  2665. in the current procedure by the name given in the corresponding
  2666. \fImyVar\fR argument.
  2667. The variable named by \fIotherVar\fR need not exist at the time of the
  2668. call;  it will be created the first time \fImyVar\fR is referenced, just like
  2669. an ordinary variable.
  2670. \fBUpvar\fR may only be invoked from within procedures.
  2671. Neither \fIotherVar\fR or \fImyVar\fR may refer to an element of an
  2672. array.
  2673. \fBUpvar\fR returns an empty string.
  2674. .RS
  2675. .LP
  2676. The \fBupvar\fR command simplifies the implementation of call-by-name
  2677. procedure calling and also makes it easier to build new control constructs
  2678. as Tcl procedures.
  2679. For example, consider the following procedure:
  2680. .DS
  2681. .ta 1c 2c 3c
  2682. \fBproc add2 name {
  2683.     upvar $name x
  2684.     set x [expr $x+2]
  2685. }
  2686. .DE
  2687. \fBAdd2\fR is invoked with an argument giving the name of a variable,
  2688. and it adds two to the value of that variable.
  2689. Although \fBadd2\fR could have been implemented using \fBuplevel\fR
  2690. instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fBadd2\fR
  2691. to access the variable in the caller's procedure frame.
  2692. .VE
  2693. .RE
  2694. .TP
  2695. \fBwhile \fItest body
  2696. .VS
  2697. The \fIwhile\fR command evaluates \fItest\fR as an expression
  2698. (in the same way that \fBexpr\fR evaluates its argument).
  2699. The value of the expression must be numeric; if it is non-zero
  2700. then \fIbody\fR is executed by passing it to the Tcl interpreter.
  2701. Once \fIbody\fR has been executed then \fItest\fR is evaluated
  2702. again, and the process repeats until eventually \fItest\fR
  2703. evaluates to a zero numeric value.  \fBContinue\fR
  2704. commands may be executed inside \fIbody\fR to terminate the current
  2705. iteration of the loop, and \fBbreak\fR
  2706. commands may be executed inside \fIbody\fR to cause immediate
  2707. termination of the \fBwhile\fR command.  The \fBwhile\fR command
  2708. always returns an empty string.
  2709. .VE
  2710.  
  2711. .SH "BUILT-IN VARIABLES"
  2712. .PP
  2713. The following global variables are created and managed automatically
  2714. by the Tcl library.  Except where noted below, these variables should
  2715. normally be treated as read-only by application-specific code and by users.
  2716. .TP
  2717. \fBenv\fR
  2718. .br
  2719. .VS
  2720. This variable is maintained by Tcl as an array
  2721. whose elements are the environment variables for the process.
  2722. Reading an element will return the value of the corresponding
  2723. environment variable.
  2724. Setting an element of the array will modify the corresponding
  2725. environment variable or create a new one if it doesn't already
  2726. exist.
  2727. Unsetting an element of \fBenv\fR will remove the corresponding
  2728. environment variable.
  2729. Changes to the \fBenv\fR array will affect the environment
  2730. passed to children by commands like \fBexec\fR.
  2731. If the entire \fBenv\fR array is unset then Tcl will stop
  2732. monitoring \fBenv\fR accesses and will not update environment
  2733. variables.
  2734. .TP
  2735. \fBerrorCode\fR
  2736. After an error has occurred, this variable will be set to hold
  2737. additional information about the error in a form that is easy
  2738. to process with programs.
  2739. \fBerrorCode\fR consists of a Tcl list with one or more elements.
  2740. The first element of the list identifies a general class of
  2741. errors, and determines the format of the rest of the list.
  2742. The following formats for \fBerrorCode\fR are used by the
  2743. Tcl core; individual applications may define additional formats.
  2744. .RS
  2745. .TP
  2746. \fBCHILDKILLED\fI pid sigName msg\fR
  2747. This format is used when a child process has been killed because of
  2748. a signal.  The second element of \fBerrorCode\fR will be the
  2749. process's identifier (in decimal).
  2750. The third element will be the symbolic name of the signal that caused
  2751. the process to terminate; it will be one of the names from the
  2752. include file signal.h, such as \fBSIGPIPE\fR.
  2753. The fourth element will be a short human-readable message
  2754. describing the signal, such as ``write on pipe with no readers''
  2755. for \fBSIGPIPE\fR.
  2756. .TP
  2757. \fBCHILDSTATUS\fI pid code\fR
  2758. This format is used when a child process has exited with a non-zero
  2759. exit status.  The second element of \fBerrorCode\fR will be the
  2760. process's identifier (in decimal) and the third element will be the exit
  2761. code returned by the process (also in decimal).
  2762. .TP
  2763. \fBCHILDSUSP\fI pid sigName msg\fR
  2764. This format is used when a child process has been suspended because
  2765. of a signal.
  2766. The second element of \fBerrorCode\fR will be the process's identifier,
  2767. in decimal.
  2768. The third element will be the symbolic name of the signal that caused
  2769. the process to suspend; this will be one of the names from the
  2770. include file signal.h, such as \fBSIGTTIN\fR.
  2771. The fourth element will be a short human-readable message
  2772. describing the signal, such as ``background tty read''
  2773. for \fBSIGTTIN\fR.
  2774. .TP
  2775. \fBNONE\fR
  2776. .br
  2777. This format is used for errors where no additional information is
  2778. available for an error besides the message returned with the
  2779. error.  In these cases \fBerrorCode\fR will consist of a list
  2780. containing a single element whose contents are \fBNONE\fR.
  2781. .TP
  2782. \fBUNIX \fIerrName msg\fR
  2783. If the first element of \fBerrorCode\fR is \fBUNIX\fR, then
  2784. the error occurred during a UNIX kernel call.
  2785. The second element of the list will contain the symbolic name
  2786. of the error that occurred, such as \fBENOENT\fR; this will
  2787. be one of the values defined in the include file errno.h.
  2788. The third element of the list will be a human-readable
  2789. message corresponding to \fIerrName\fR, such as
  2790. ``no such file or directory'' for the \fBENOENT\fR case.
  2791. .PP
  2792. To set \fBerrorCode\fR, applications should use library
  2793. procedures such as \fBTcl_SetErrorCode\fR and
  2794. \fBTcl_UnixError\fR, or they may invoke the \fBerror\fR command.
  2795. If one of these methods hasn't been used, then the Tcl
  2796. interpreter will reset the variable to \fBNONE\fR after
  2797. the next error.
  2798. .RE
  2799. .VE
  2800. .TP
  2801. \fBerrorInfo\fR
  2802. After an error has occurred, this string will contain one or more lines
  2803. identifying the Tcl commands and procedures that were being executed
  2804. when the most recent error occurred.
  2805. Its contents take the form of a stack trace showing the various
  2806. nested Tcl commands that had been invoked at the time of the error.
  2807.  
  2808. .SH AUTHOR
  2809. John Ousterhout, University of California at Berkeley (ouster@sprite.berkeley.edu)
  2810. .sp
  2811. Many people have contributed to Tcl in various ways, but the following
  2812. people have made unusually large contributions:
  2813. .sp
  2814. .nf
  2815. Bill Carpenter
  2816. Peter Da Silva
  2817. Mark Diekhans
  2818. Karl Lehenbauer
  2819. Mary Ann May-Pumphrey
  2820.